From: Brett Parker Date: Tue, 8 Dec 2020 14:18:42 +0000 (+0000) Subject: Day 8 2020 X-Git-Url: https://git.sommitrealweird.co.uk/advent-of-code-2020.git/commitdiff_plain/6d883615205b7c9f0693d0c2f9a4cc95590a70c6?hp=b33a1377092119697b3ab349a7a4c066c7471879 Day 8 2020 --- diff --git a/day08/computer.py b/day08/computer.py new file mode 100644 index 0000000..9243eea --- /dev/null +++ b/day08/computer.py @@ -0,0 +1,64 @@ +#!/usr/bin/python3 + +class Computer: + + def __init__(self, instructions=[], break_on_exec=0): + self.__acc__=0 + self.__pos__=0 + self.__instructions__=instructions + self.__executed_count__={} + self.__keep_running__=True + self.__break_on_exec_number__=break_on_exec + self.__exited_on_break__=False + + def set_instructions(self, instructions): + self.__instructions__=instructions + + def run(self): + while self.keep_running(): + self.run_command() + # when we're done, return the accumulator + return self.__acc__ + + def keep_running(self): + if self.__pos__ >= len(self.__instructions__): + self.__keep_running__=False + return self.__keep_running__ + + def exited_on_break(self): + return self.__exited_on_break__ + + def run_command(self): + if self.__pos__ in self.__executed_count__: + self.__executed_count__[self.__pos__]+=1 + else: + self.__executed_count__[self.__pos__]=1 + + if self.__break_on_exec_number__ > 0 and self.__executed_count__[self.__pos__] >= self.__break_on_exec_number__: + self.__keep_running__=False + self.__exited_on_break__=True + return + else: + (instruction,param) = self.__instructions__[self.__pos__] + if instruction == "nop": + # do nothing, except increment the program counter + self.__pos__+=1 + elif instruction == "acc": + val=int(param) + self.__acc__+=val + self.__pos__+=1 + elif instruction == "jmp": + val=int(param) + self.__pos__+=val + else: + raise Exception("WTAF - that's not a real instruction!") + return + +def main(): + instructions = [line.rstrip().split(" ") for line in open("input.txt", "r")] + comp = Computer(instructions, break_on_exec=2) + + print("Acc was {} when computer finished.".format(comp.run())) + +if __name__ == "__main__": + main() diff --git a/day08/input-example-part2-exit-8.txt b/day08/input-example-part2-exit-8.txt new file mode 100644 index 0000000..178df53 --- /dev/null +++ b/day08/input-example-part2-exit-8.txt @@ -0,0 +1,9 @@ +nop +0 +acc +1 +jmp +4 +acc +3 +jmp -3 +acc -99 +acc +1 +jmp -4 +acc +6 diff --git a/day08/input.txt b/day08/input.txt new file mode 100644 index 0000000..11cfbaa --- /dev/null +++ b/day08/input.txt @@ -0,0 +1,625 @@ +acc +17 +acc +37 +acc -13 +jmp +173 +nop +100 +acc -7 +jmp +447 +nop +283 +acc +41 +acc +32 +jmp +1 +jmp +585 +jmp +1 +acc -5 +nop +71 +acc +49 +acc -18 +jmp +527 +jmp +130 +jmp +253 +acc +11 +acc -11 +jmp +390 +jmp +597 +jmp +1 +acc +6 +acc +0 +jmp +588 +acc -17 +jmp +277 +acc +2 +nop +163 +jmp +558 +acc +38 +jmp +369 +acc +13 +jmp +536 +acc +38 +acc +39 +acc +6 +jmp +84 +acc +11 +nop +517 +acc +48 +acc +47 +jmp +1 +acc +42 +acc +0 +acc +2 +acc +24 +jmp +335 +acc +44 +acc +47 +jmp +446 +nop +42 +nop +74 +acc +45 +jmp +548 +jmp +66 +acc +1 +jmp +212 +acc +18 +jmp +1 +acc +4 +acc -16 +jmp +366 +acc +0 +jmp +398 +acc +45 +jmp +93 +acc +40 +acc +38 +acc +21 +nop +184 +jmp -46 +nop -9 +jmp +53 +acc +46 +acc +36 +jmp +368 +acc +16 +acc +8 +acc -9 +acc -4 +jmp +328 +acc -15 +acc -5 +acc +21 +jmp +435 +acc -5 +acc +36 +jmp +362 +acc +26 +jmp +447 +jmp +1 +jmp +412 +acc +11 +acc +41 +nop -32 +acc +17 +jmp -63 +jmp +1 +nop +393 +jmp +62 +acc +18 +acc +30 +nop +417 +jmp +74 +acc +29 +acc +23 +jmp +455 +jmp +396 +jmp +395 +acc +33 +nop +137 +nop +42 +jmp +57 +jmp +396 +acc +7 +acc +0 +jmp +354 +acc +15 +acc +50 +jmp -12 +jmp +84 +nop +175 +acc +5 +acc -2 +jmp -82 +acc +1 +acc +26 +jmp +288 +nop -113 +nop +366 +acc +45 +jmp +388 +acc +21 +acc +38 +jmp +427 +acc +33 +jmp -94 +nop -118 +nop +411 +jmp +472 +nop +231 +nop +470 +acc +48 +jmp -124 +jmp +1 +acc +5 +acc +37 +acc +42 +jmp +301 +acc -11 +acc -17 +acc +14 +jmp +357 +acc +6 +acc +20 +acc +13 +jmp +361 +jmp -65 +acc +29 +jmp +26 +jmp +329 +acc +32 +acc +32 +acc +17 +jmp -102 +acc -6 +acc +33 +acc +9 +jmp +189 +acc +3 +jmp -128 +jmp -142 +acc +24 +acc -5 +jmp +403 +acc +28 +jmp +310 +acc +34 +acc +4 +acc +33 +acc +18 +jmp +227 +acc -8 +acc -15 +jmp +112 +jmp +54 +acc +21 +acc +23 +acc +20 +jmp +320 +acc +13 +jmp -77 +acc +15 +nop +310 +nop +335 +jmp +232 +acc -3 +nop +50 +acc +41 +jmp +112 +nop -10 +acc +29 +acc +27 +jmp +52 +acc +40 +nop -132 +acc -16 +acc +27 +jmp +309 +acc -8 +nop +147 +acc +20 +acc +46 +jmp +202 +acc +27 +jmp -43 +jmp +1 +acc +33 +acc -13 +jmp +300 +acc +1 +jmp -202 +acc -17 +acc +0 +acc +34 +jmp -5 +nop +335 +acc -16 +acc -17 +jmp -120 +acc -19 +acc -13 +acc +4 +jmp +368 +jmp +21 +acc +39 +acc +39 +acc -18 +jmp -157 +nop +280 +acc +33 +nop -37 +jmp +32 +acc -16 +acc +18 +acc +46 +jmp -121 +acc -19 +jmp +195 +acc +28 +jmp +124 +jmp +331 +jmp -228 +jmp -146 +jmp +85 +jmp +60 +acc +20 +acc -9 +jmp +303 +jmp -122 +jmp +111 +acc +32 +acc +0 +acc +39 +acc +29 +jmp -31 +nop +320 +jmp -63 +jmp +223 +nop -149 +acc -12 +acc -11 +acc +32 +jmp +309 +jmp -13 +acc -19 +jmp -123 +acc +21 +acc +18 +acc +49 +jmp +175 +acc -14 +nop -129 +acc -2 +acc +31 +jmp +79 +acc +23 +acc +50 +acc +39 +acc +7 +jmp -235 +jmp -166 +acc +9 +jmp +293 +acc -11 +jmp +76 +acc +44 +acc +3 +acc +37 +jmp +123 +nop -104 +jmp -157 +acc +14 +acc +10 +acc +28 +jmp +25 +acc +37 +jmp +188 +jmp -49 +acc -11 +jmp -90 +acc -8 +jmp +197 +acc +5 +jmp +115 +acc +44 +jmp -228 +nop -2 +acc +46 +jmp +130 +nop +183 +nop +106 +acc +27 +acc +37 +jmp -309 +acc +28 +acc -4 +acc -12 +acc +38 +jmp +93 +acc +8 +acc +23 +acc -9 +acc +6 +jmp -42 +acc +10 +acc +35 +acc +4 +jmp -231 +acc +19 +acc +7 +acc +23 +acc +11 +jmp -90 +acc +0 +nop +158 +nop -150 +acc +33 +jmp +107 +acc +48 +acc -2 +jmp -104 +acc +6 +nop -57 +nop +172 +acc -11 +jmp -7 +acc +6 +acc +50 +acc -9 +acc +12 +jmp -171 +acc +3 +jmp +26 +acc +42 +acc +31 +acc +20 +acc +32 +jmp -48 +acc +13 +jmp -6 +jmp +178 +acc +47 +jmp -153 +acc +28 +nop +74 +jmp -162 +acc -15 +nop -104 +acc -9 +jmp -227 +acc +49 +acc -19 +acc +41 +jmp -318 +acc +9 +acc +12 +acc +7 +jmp +34 +jmp +137 +nop -143 +acc -8 +acc +5 +acc +31 +jmp -20 +jmp -237 +acc +39 +acc +0 +jmp -298 +acc +45 +acc -19 +acc +11 +jmp -151 +acc +40 +acc +27 +nop +150 +nop -391 +jmp -341 +acc +1 +acc +11 +acc +18 +nop -234 +jmp +77 +nop +104 +jmp -65 +acc +32 +jmp -27 +nop -317 +nop +159 +acc +14 +acc -10 +jmp -348 +acc +29 +jmp +32 +acc +48 +acc -19 +jmp +17 +jmp -201 +jmp -224 +nop +26 +acc -7 +acc +23 +acc +46 +jmp -6 +acc +22 +acc +39 +acc +9 +acc +23 +jmp -30 +jmp -243 +acc +47 +acc -15 +jmp -298 +jmp -393 +jmp +1 +acc +3 +nop -24 +acc +7 +jmp -59 +acc -6 +acc +26 +jmp -102 +acc +34 +acc +24 +jmp -207 +acc +36 +acc +40 +acc +41 +jmp +1 +jmp -306 +jmp +57 +jmp +1 +nop +99 +acc +28 +jmp -391 +acc +50 +jmp -359 +acc -5 +jmp +9 +jmp -355 +acc +5 +acc +2 +jmp -77 +acc +40 +acc +28 +acc +22 +jmp -262 +nop -287 +acc +34 +acc -4 +nop +112 +jmp -195 +acc +29 +nop -94 +nop -418 +jmp +24 +jmp -190 +acc +2 +jmp -311 +jmp -178 +jmp -276 +acc -12 +acc -18 +jmp +62 +jmp -174 +nop +31 +acc +33 +nop -158 +jmp -417 +acc +3 +acc +21 +acc +47 +jmp +87 +acc +45 +jmp -77 +acc +6 +acc -10 +jmp +1 +jmp -240 +acc +7 +acc +47 +jmp -379 +acc -14 +acc +50 +nop -75 +acc +30 +jmp +70 +jmp -392 +jmp -430 +acc +22 +acc -2 +jmp -492 +jmp +1 +acc -6 +acc +38 +jmp -36 +nop -336 +jmp -32 +jmp +61 +acc +20 +acc -9 +acc +2 +jmp -175 +acc +21 +acc -2 +jmp -6 +jmp -527 +acc +11 +acc +16 +jmp -262 +jmp +1 +nop -327 +acc +29 +jmp -114 +acc +11 +acc +17 +acc +26 +nop -104 +jmp -428 +nop -178 +nop -242 +acc +29 +acc +5 +jmp -245 +jmp -417 +jmp -278 +acc +35 +acc +21 +jmp +1 +nop -263 +jmp +8 +acc +42 +jmp -95 +nop -312 +acc -11 +acc +34 +acc +0 +jmp +19 +acc +8 +acc -13 +acc +32 +acc +21 +jmp -208 +acc +15 +acc +39 +nop -194 +jmp -280 +jmp +24 +nop -516 +acc +21 +acc +48 +jmp -367 +jmp -121 +acc +49 +acc -16 +jmp -136 +acc +0 +jmp -148 +jmp -85 +jmp -103 +nop -446 +jmp -242 +acc -12 +acc +13 +acc +31 +acc -1 +jmp -435 +nop -420 +acc +22 +acc -5 +jmp -567 +nop -354 +acc +11 +acc +33 +acc +45 +jmp -76 +acc -2 +acc +0 +acc +25 +acc +46 +jmp -555 +acc +0 +acc +11 +nop -2 +jmp -394 +jmp -395 +acc +8 +acc +14 +acc +47 +acc +22 +jmp +1 diff --git a/day08/part2.py b/day08/part2.py new file mode 100644 index 0000000..e45c74d --- /dev/null +++ b/day08/part2.py @@ -0,0 +1,42 @@ +#!/usr/bin/python + +import computer +import copy + +def get_possible_change_locations(instructions): + change_locations=[] + instruction_location=0 + for instruction,param in instructions: + if instruction == "nop": + change_locations.append(instruction_location) + elif instruction == "jmp": + change_locations.append(instruction_location) + instruction_location+=1 + return change_locations + +def main(): + # read the instructions + instructions=[line.rstrip().split(" ") for line in open("input.txt", "r")] + possible_change_locations=get_possible_change_locations(instructions) + + for change_location in possible_change_locations: + new_instructions=copy.deepcopy(instructions) + instruction = new_instructions[change_location][0] + param = new_instructions[change_location][1] + if instruction == "nop": + instruction = "jmp" + else: + instruction = "nop" + + new_instructions[change_location][0]=instruction + + comp=computer.Computer(new_instructions,break_on_exec=10) + acc=comp.run() + if comp.exited_on_break(): + continue + else: + print ("Replaced instruction at pos: {}, computer terminated normally with acc: {}".format(change_location, acc)) + break + +if __name__ == "__main__": + main() diff --git a/day08/summary.txt b/day08/summary.txt new file mode 100644 index 0000000..a5a0130 --- /dev/null +++ b/day08/summary.txt @@ -0,0 +1,88 @@ +--- 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, or nop) and an argument (a signed number like +4 or -20). + +acc increases or decreases a single global value called the accumulator by the value given in the argument. For example, acc +7 would increase the accumulator by 7. The accumulator starts at 0. After an acc instruction, the instruction immediately below it is executed next. +jmp jumps to a new instruction relative to itself. The next instruction to execute is found using the argument as an offset from the jmp instruction; for example, jmp +2 would skip the next instruction, jmp +1 would continue to the instruction immediately below it, and jmp -20 would cause the instruction 20 lines above to be executed next. +nop stands for No OPeration - it does nothing. The instruction immediately below it is executed next. +For example, consider the following program: + +nop +0 +acc +1 +jmp +4 +acc +3 +jmp -3 +acc -99 +acc +1 +jmp -4 +acc +6 +These instructions are visited in this order: + +nop +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 | +First, the nop +0 does nothing. Then, the accumulator is increased from 0 to 1 (acc +1) and jmp +4 sets the next instruction to the other acc +1 near the bottom. After it increases the accumulator from 1 to 2, jmp -4 executes, setting the next instruction to the only acc +3. It sets the accumulator to 5, and jmp -3 causes the program to continue back at the first acc +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? + +Your puzzle answer was 1797. + +--- Part Two --- +After some careful analysis, you believe that exactly one instruction is corrupted. + +Somewhere in the program, either a jmp is supposed to be a nop, or a nop is supposed to be a jmp. (No acc instructions 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 jmp or nop, you can repair the boot code and make it terminate correctly. + +For example, consider the same program from above: + +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! The instructions are visited in this order: + +nop +0 | 1 +acc +1 | 2 +jmp +4 | 3 +acc +3 | +jmp -3 | +acc -99 | +acc +1 | 4 +nop -4 | 5 +acc +6 | 6 +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). + +Fix the program so that it terminates normally by changing exactly one jmp (to nop) or nop (to jmp). What is the value of the accumulator after the program terminates? + +Your puzzle answer was 1036. + +Both parts of this puzzle are complete! They provide two gold stars: ** + +At this point, you should return to your Advent calendar and try another puzzle. + +If you still want to see it, you can get your puzzle input. + +You can also [Share] this puzzle.