From ad0e906acf7c3b31710415bb5c7f7d8199bd5146 Mon Sep 17 00:00:00 2001 From: Ben Charlton Date: Tue, 8 Dec 2020 09:10:44 +0000 Subject: [PATCH] day 8 --- 08/08.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 08/08.py diff --git a/08/08.py b/08/08.py new file mode 100644 index 0000000..7b768f7 --- /dev/null +++ b/08/08.py @@ -0,0 +1,62 @@ +#!/usr/bin/python3 + +import sys +import copy + +instructions = [] +for line in sys.stdin: + inst = line.rstrip().split(' ') + instructions.append(inst) + +def exec_instructions(instructions): + acc = 0 + pc = 0 + seen = {} + while pc < len(instructions): + if pc in seen: + return acc + + seen[pc] = 1 + + inst = instructions[pc] + if inst[0] == 'jmp': + pc = pc + int(inst[1]) + continue + elif inst[0] == 'acc': + acc = acc + int(inst[1]) + pc = pc + 1 + +def mod_instructions(instructions, pos): + acc = 0 + pc = 0 + seen = {} + + if instructions[pos][0] == 'jmp': + instructions[pos][0] = 'nop' + elif instructions[pos][0] == 'nop': + instructions[pos][0] = 'jmp' + + while pc < len(instructions): + if pc in seen: + return None + + seen[pc] = 1 + + inst = instructions[pc] + if inst[0] == 'jmp': + pc = pc + int(inst[1]) + continue + elif inst[0] == 'acc': + acc = acc + int(inst[1]) + + pc = pc + 1 + return acc + +print(1,exec_instructions(instructions)) + +for p in range(len(instructions)): + i = copy.deepcopy(instructions) + r = mod_instructions(i, p) + if r is not None: + print(2,r) + sys.exit \ No newline at end of file