diff options
author | Yigit Sever | 2021-12-13 10:38:11 +0300 |
---|---|---|
committer | Yigit Sever | 2021-12-13 10:38:11 +0300 |
commit | 74b27ccca31bb757c737dd7fdc02f513f57561b2 (patch) | |
tree | e27db4cd0873c81a53d32277446d926d176304e0 /2020/day8/gamechild.py | |
parent | 3919f90cfbfbba26c8e39f979280649f5e08aea8 (diff) | |
parent | ac8125750abed263619da4cc6d653bb5ab76f007 (diff) | |
download | aoc-74b27ccca31bb757c737dd7fdc02f513f57561b2.tar.gz aoc-74b27ccca31bb757c737dd7fdc02f513f57561b2.tar.bz2 aoc-74b27ccca31bb757c737dd7fdc02f513f57561b2.zip |
Merge remote-tracking branch 'origin/main'
Diffstat (limited to '2020/day8/gamechild.py')
-rw-r--r-- | 2020/day8/gamechild.py | 55 |
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 @@ | |||
1 | boot_code = list() | ||
2 | |||
3 | |||
4 | def 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 | |||
24 | with 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 | |||
31 | cursor = 0 | ||
32 | accumulator = 0 | ||
33 | sofar = 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 | |||
38 | while 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 | ||