X-Git-Url: https://git.sommitrealweird.co.uk/advent-of-code-2020.git/blobdiff_plain/b33a1377092119697b3ab349a7a4c066c7471879..6d883615205b7c9f0693d0c2f9a4cc95590a70c6:/day08/part2.py 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()