From 4bb6f8d06c0e384f3394012b1d48da58ed28cc5e Mon Sep 17 00:00:00 2001 From: Yigit Sever Date: Sun, 12 Dec 2021 01:24:32 +0300 Subject: 2020, tracking --- 2020/day8/gamechild.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 2020/day8/gamechild.py (limited to '2020/day8/gamechild.py') diff --git a/2020/day8/gamechild.py b/2020/day8/gamechild.py new file mode 100644 index 0000000..3763c78 --- /dev/null +++ b/2020/day8/gamechild.py @@ -0,0 +1,55 @@ +boot_code = list() + + +def gameon(acc, cx, sf): + """emulates the game until a loop or the end""" + + while cx not in sf: + sf.add(cx) + + if cx == len(boot_code): + print(acc) + exit(0) + + if boot_code[cx][0] == "acc": + acc += boot_code[cx][1] + elif boot_code[cx][0] == "jmp": + cx += boot_code[cx][1] - 1 + else: # nop + pass + + cx += 1 + + +with open("input", "r") as file: + for line in file: + line = line.strip() + oparg = line.split(" ") + boot_code.append((oparg[0], int(oparg[1]))) + + +cursor = 0 +accumulator = 0 +sofar = set() + +# we are allowed to keep a 'main' branch and keep jmp/nops separate because +# task calls for only one instruction change over the whole code + +while cursor not in sofar: + sofar.add(cursor) + + if boot_code[cursor][0] == "acc": + accumulator += boot_code[cursor][1] + elif boot_code[cursor][0] == "jmp": + # one for the jmp + gameon(accumulator, cursor + boot_code[cursor][1], sofar.copy()) + # one for the nop + gameon(accumulator, cursor + 1, sofar.copy()) + cursor += boot_code[cursor][1] - 1 + elif boot_code[cursor][0] == "nop": + # one for the nop + gameon(accumulator, cursor + 1, sofar.copy()) + # one for the jmp + gameon(accumulator, cursor + boot_code[cursor][1], sofar.copy()) + + cursor += 1 -- cgit v1.2.3-70-g09d2