Skip to content

Commit 0b70500

Browse files
committed
AoC 2020, day 8
1 parent 6034f1e commit 0b70500

File tree

3 files changed

+668
-0
lines changed

3 files changed

+668
-0
lines changed

2020/08/example

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
nop +0
2+
acc +1
3+
jmp +4
4+
acc +3
5+
jmp -3
6+
acc -99
7+
acc +1
8+
jmp -4
9+
acc +6

2020/08/halting.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/python3
2+
def debug(code):
3+
for pc in range(0, len(code)):
4+
operation, argument = code[pc]
5+
if operation == 'nop':
6+
code[pc][0] = 'jmp'
7+
elif operation == 'jmp':
8+
code[pc][0] = 'nop'
9+
return_value = execute(code, halt=True, debug=True)
10+
if return_value:
11+
return return_value
12+
else:
13+
code[pc][0] = operation
14+
15+
def execute(code, halt=False, debug=False):
16+
accumulator = 0
17+
pc = 0
18+
executed = set()
19+
20+
while True:
21+
if halt:
22+
if pc in executed:
23+
if debug:
24+
return False
25+
break
26+
executed.add(pc)
27+
28+
if pc == len(code):
29+
break
30+
31+
operation, argument = code[pc]
32+
33+
if operation == 'acc':
34+
accumulator += int(argument)
35+
pc += 1
36+
elif operation == 'jmp':
37+
pc += int(argument)
38+
else:
39+
pc += 1
40+
41+
return accumulator
42+
43+
def main():
44+
with open('input') as f:
45+
code = [line.strip().split(' ') for line in f]
46+
47+
print(execute(code, halt=True))
48+
print(debug(code))
49+
50+
if __name__ == "__main__":
51+
main()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy