summaryrefslogtreecommitdiffstats
path: root/2020/day8/gamechild.py
diff options
context:
space:
mode:
Diffstat (limited to '2020/day8/gamechild.py')
-rw-r--r--2020/day8/gamechild.py55
1 files changed, 55 insertions, 0 deletions
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 @@
1boot_code = list()
2
3
4def gameon(acc, cx, sf):
5 """emulates the game until a loop or the end"""
6
7 while cx not in sf:
8 sf.add(cx)
9
10 if cx == len(boot_code):
11 print(acc)
12 exit(0)
13
14 if boot_code[cx][0] == "acc":
15 acc += boot_code[cx][1]
16 elif boot_code[cx][0] == "jmp":
17 cx += boot_code[cx][1] - 1
18 else: # nop
19 pass
20
21 cx += 1
22
23
24with open("input", "r") as file:
25 for line in file:
26 line = line.strip()
27 oparg = line.split(" ")
28 boot_code.append((oparg[0], int(oparg[1])))
29
30
31cursor = 0
32accumulator = 0
33sofar = set()
34
35# we are allowed to keep a 'main' branch and keep jmp/nops separate because
36# task calls for only one instruction change over the whole code
37
38while cursor not in sofar:
39 sofar.add(cursor)
40
41 if boot_code[cursor][0] == "acc":
42 accumulator += boot_code[cursor][1]
43 elif boot_code[cursor][0] == "jmp":
44 # one for the jmp
45 gameon(accumulator, cursor + boot_code[cursor][1], sofar.copy())
46 # one for the nop
47 gameon(accumulator, cursor + 1, sofar.copy())
48 cursor += boot_code[cursor][1] - 1
49 elif boot_code[cursor][0] == "nop":
50 # one for the nop
51 gameon(accumulator, cursor + 1, sofar.copy())
52 # one for the jmp
53 gameon(accumulator, cursor + boot_code[cursor][1], sofar.copy())
54
55 cursor += 1