Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Day 8: Handheld Halting
Your flight to the major airline hub reaches cruising altitude without incident. While you consider checking the in-flight menu for one of those drinks that come with a little umbrella, you are interrupted by the kid sitting next to you.
Their handheld game console won't turn on! They ask if you can take a look.
You narrow the problem down to a strange infinite loop in the boot code (your puzzle input) of the device. You should be able to fix it, but first you need to be able to run the code in isolation.
The boot code is represented as a text file with one instruction per line of text. Each instruction consists of an operation (
acc,jmp, ornop) and an argument (a signed number like+4or-20).accincreases or decreases a single global value called the accumulator by the value given in the argument. For example,acc +7would increase the accumulator by 7. The accumulator starts at0. After anaccinstruction, the instruction immediately below it is executed next.jmpjumps to a new instruction relative to itself. The next instruction to execute is found using the argument as an offset from thejmpinstruction; for example,jmp +2would skip the next instruction,jmp +1would continue to the instruction immediately below it, andjmp -20would cause the instruction 20 lines above to be executed next.nopstands for No OPeration - it does nothing. The instruction immediately below it is executed next.For example, consider the following program:
These instructions are visited in this order:
First, the
nop +0does nothing. Then, the accumulator is increased from 0 to 1 (acc +1) andjmp +4sets the next instruction to the otheracc +1near the bottom. After it increases the accumulator from 1 to 2,jmp -4executes, setting the next instruction to the onlyacc +3. It sets the accumulator to 5, andjmp -3causes the program to continue back at the firstacc +1.This is an infinite loop : with this sequence of jumps, the program will run forever. The moment the program tries to run any instruction a second time, you know it will never terminate.
Immediately before the program would run an instruction a second time, the value in the accumulator is
5.Run your copy of the boot code. Immediately before any instruction is executed a second time, what value is in the accumulator?
Part Two
After some careful analysis, you believe that exactly one instruction is corrupted .
Somewhere in the program, either a
jmpis supposed to be anop, or anopis supposed to be ajmp. (Noaccinstructions were harmed in the corruption of this boot code.)The program is supposed to terminate by attempting to execute an instruction immediately after the last instruction in the file . By changing exactly one
jmpornop, you can repair the boot code and make it terminate correctly.For example, consider the same program from above:
If you change the first instruction from
nop +0tojmp +0, it would create a single-instruction infinite loop, never leaving that instruction. If you change almost any of thejmpinstructions, the program will still eventually find anotherjmpinstruction and loop forever.However, if you change the second-to-last instruction (from
jmp -4tonop -4), the program terminates! The instructions are visited in this order:After the last instruction (
acc +6), the program terminates by attempting to run the instruction below the last instruction in the file. With this change, after the program terminates, the accumulator contains the value8(acc +1,acc +1,acc +6).Fix the program so that it terminates normally by changing exactly one
jmp(tonop) ornop(tojmp). What is the value of the accumulator after the program terminates?Link
https://adventofcode.com/2020/day/8