-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8.py
executable file
·60 lines (45 loc) · 1.18 KB
/
day8.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3
# [Day 8: Handheld Halting](https://adventofcode.com/2020/day/8)
import sys
from pathlib import Path
verbose = "-v" in sys.argv
if verbose:
sys.argv.remove("-v")
filename = ("test.txt" if sys.argv[1] == "-t" else sys.argv[1]) if len(sys.argv) > 1 else "input.txt"
data = Path(filename).read_text().strip()
lines = data.splitlines()
def run(program):
acc = 0
ip = 0
visited = set()
while ip < len(program) and ip not in visited:
visited.add(ip)
instr = program[ip]
ip += 1
op, imm = instr.split(maxsplit=1)
if op == "nop":
pass
elif op == "acc":
acc += int(imm)
elif op == "jmp":
ip += int(imm) - 1
else:
raise ValueError(instr)
return acc, ip == len(program)
# part 1
acc, booted = run(lines)
assert booted is False
print(acc)
# part 2
for i in range(len(lines)):
backup = lines[i]
instr, imm = backup.split(maxsplit=1)
if instr == "nop":
lines[i] = f"jmp {imm}"
elif instr == "jmp":
lines[i] = f"nop {imm}"
acc, booted = run(lines)
if booted:
break
lines[i] = backup
print(acc)