logo CodeStepByStep logo

BootCode2

Write a method named BootCode2 (similar to the previous exercise, bootCode). In this exercise, your method is searching for exactly one instruction that has been corrupted, and returning the accumulator value that would result from repairing that corrupted instruction. Specifically, somewhere in the program, either a jmp is supposed to be a nop, or a nop is supposed to be a jmp.

The program is supposed to terminate by attempting to execute an instruction immediately after the last instruction in the file. By changing exactly one jmp or nop, you can repair the boot code and make it terminate correctly. For example, suppose the file named code.txt contains the following values:

nop +0
acc +1
jmp +4
acc +3
jmp -3
acc -99
acc +1
jmp -4
acc +6

If you change the first instruction from nop +0 to jmp +0, it would create a single-instruction infinite loop, never leaving that instruction. If you change almost any of the jmp instructions, the program will still eventually find another jmp instruction and loop forever. However, if you change the second-to-last instruction (from jmp -4 to nop -4), the program terminates. 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 value 8 (acc +1, acc +1, acc +6). So the call of BootCode2("code.txt") should return 8 since that would be its last value before termination after this change.

(This exercise is based on the Advent of Code 2020, day 8.)

Method: Write a C# method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.