-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08.py
74 lines (57 loc) · 1.57 KB
/
day08.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from util import Day
from aocd import submit
def preprocess(row):
op, val = row.strip().split()
return op, int(val)
def flipper(instructions):
for i, instruction in enumerate(instructions):
fixed = instructions.copy()
if instruction[0] == "nop":
fixed[i] = ("jmp", instruction[1])
elif instruction[0] == "jmp":
fixed[i] = ("nop", instruction[1])
else:
continue
status, val = intcode(fixed)
if status == 0:
return val
return "Could not fix input."
def intcode(instructions):
n_instructions = len(instructions)
pointer = 0
accumulator = 0
history = []
while True:
if pointer in history:
return 1, accumulator
else:
history.append(pointer)
op, val = instructions[pointer]
if op == "nop":
pointer += 1
elif op == "acc":
pointer += 1
accumulator += val
elif op == "jmp":
pointer += val
if pointer == n_instructions:
return (0, accumulator)
pointer = pointer % n_instructions
def main(day, part=1):
day.apply(preprocess)
if part == 1:
out = intcode(day.data)[1]
if part == 2:
out = flipper(day.data)
return out
if __name__ == "__main__":
day = Day(8)
day.download()
day.load(typing=str)
p1 = main(day)
print(p1)
submit(p1, part="a", day=8, year=2020)
day.load(typing=str)
p2 = main(day, part=2)
print(p2)
submit(p2, part="b", day=8, year=2020)