part1 (2684B)
1--- Day 8: Handheld Halting --- 2 3Your flight to the major airline hub reaches cruising altitude without incident. While you consider 4checking the in-flight menu for one of those drinks that come with a little umbrella, you are 5interrupted by the kid sitting next to you. 6 7Their handheld game console won't turn on! They ask if you can take a look. 8 9You narrow the problem down to a strange [1m[37minfinite loop[0m in the boot code (your puzzle 10input) of the device. You should be able to fix it, but first you need to be able to run the code in 11isolation. 12 13The boot code is represented as a text file with one [1m[37minstruction[0m per line of text. Each 14instruction consists of an [1m[37moperation[0m (acc, jmp, or nop) and an [1m[37margument[0m (a 15signed number like +4 or -20). 16 17- acc increases or decreases a single global value called the [1m[37maccumulator[0m by the value 18given in the argument. For example, acc +7 would increase the accumulator by 7. The accumulator 19starts at 0. After an acc instruction, the instruction immediately below it is executed next. - jmp 20[1m[37mjumps[0m to a new instruction relative to itself. The next instruction to execute is found 21using the argument as an [1m[37moffset[0m from the jmp instruction; for example, jmp +2 would 22skip the next instruction, jmp +1 would continue to the instruction immediately below it, and jmp 23-20 would cause the instruction 20 lines above to be executed next. - nop stands for [1m[37mNo 24OPeration[0m - it does nothing. The instruction immediately below it is executed next. 25 26For example, consider the following program: 27 28nop +0 acc +1 jmp +4 acc +3 jmp -3 acc -99 acc +1 jmp -4 acc +6 29 30These instructions are visited in this order: 31 32nop +0 | 1 acc +1 | 2, 8(!) jmp +4 | 3 acc +3 | 6 jmp -3 | 7 acc -99 | acc +1 | 4 jmp -4 | 5 acc +6 33| 34 35First, the nop +0 does nothing. Then, the accumulator is increased from 0 to 1 (acc +1) and jmp +4 36sets the next instruction to the other acc +1 near the bottom. After it increases the accumulator 37from 1 to 2, jmp -4 executes, setting the next instruction to the only acc +3. It sets the 38accumulator to 5, and jmp -3 causes the program to continue back at the first acc +1. 39 40This is an [1m[37minfinite loop[0m: with this sequence of jumps, the program will run forever. 41The moment the program tries to run any instruction a second time, you know it will never terminate. 42 43Immediately [1m[37mbefore[0m the program would run an instruction a second time, the value in the 44accumulator is [1m[37m5[0m. 45 46Run your copy of the boot code. Immediately before any instruction is executed a second time, 47[1m[37mwhat value is in the accumulator?[0m 48 49