Day 8 2020
[advent-of-code-2020.git] / day08 / part2.py
diff --git a/day08/part2.py b/day08/part2.py
new file mode 100644 (file)
index 0000000..e45c74d
--- /dev/null
@@ -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()