From 4cc675c580ec24d67c66d2b5a65266b7c036a312 Mon Sep 17 00:00:00 2001 From: npc-strider Date: Tue, 8 Dec 2020 16:06:47 +0800 Subject: [PATCH] advent of code day 8 --- 8/a.py | 22 ++++++++++++++++++++++ 8/b.py | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 8/a.py create mode 100644 8/b.py diff --git a/8/a.py b/8/a.py new file mode 100644 index 0000000..4c5ce26 --- /dev/null +++ b/8/a.py @@ -0,0 +1,22 @@ + +ACC = 0 +INDEX = 0 + +indices = [] + +lines = [ [y[0],int(y[1])] for x in open('input').read().splitlines() if (y := x.split(' ')) ] + +while INDEX not in indices: + instruction = lines[INDEX][0] + arg0 = lines[INDEX][1] + print(instruction + ' ' + str(arg0) + ' ' + str(ACC) + ' ' + str(INDEX) ) + if instruction == 'nop': + True + elif instruction == 'acc': + ACC += arg0 + elif instruction == 'jmp': + INDEX += arg0 - 1 + indices.append(INDEX) + INDEX += 1 + + \ No newline at end of file diff --git a/8/b.py b/8/b.py new file mode 100644 index 0000000..2d2ca99 --- /dev/null +++ b/8/b.py @@ -0,0 +1,41 @@ +import copy + +instructions = [ [y[0],int(y[1])] for x in open('input').read().splitlines() if (y := x.split(' ')) ] + +def interpreter(instructions): + ACC = 0 + INDEX = 0 + indices = [] + while True: + if INDEX in indices: + return False + elif INDEX == len(instructions): + return ACC + # return True + else: + instruction = instructions[INDEX][0] + arg0 = instructions[INDEX][1] + # print(instruction + ' ' + str(arg0) + ' ' + str(ACC) + ' ' + str(INDEX) ) + if instruction == 'nop': + True + elif instruction == 'acc': + ACC += arg0 + elif instruction == 'jmp': + INDEX += arg0 - 1 + indices.append(INDEX) + INDEX += 1 + +for i in range(0,len(instructions)): + instructions_ = copy.deepcopy(instructions) + instruction = instructions_[i][0] + if instruction == 'jmp': + instructions_[i][0] = 'nop' + print('jmp -> nop ' + str(i)) + elif instruction == 'nop' and instructions_[i][1] != 0: + print('nop -> jmp ' + str(i)) + instructions_[i][0] = 'jmp' + + c = interpreter(instructions_) + if c != False: + print(c) #758??? + break