-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday_08.py
More file actions
77 lines (64 loc) · 1.74 KB
/
day_08.py
File metadata and controls
77 lines (64 loc) · 1.74 KB
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
75
76
77
#%% Part 1
with open("day_08_input.txt") as input_data:
op_codes = [x.strip().split() for x in input_data]
position = 0
visited = set()
accumulator = 0
class LoopException(Exception):
pass
def noop(*args) -> None:
global position
if position in visited:
raise LoopException
visited.add(position)
position += 1
def acc(value: int) -> None:
global position
global accumulator
if position in visited:
raise LoopException
visited.add(position)
accumulator += value
position += 1
def jmp(value: int) -> None:
global position
if position in visited:
raise LoopException
visited.add(position)
position += value
op_map = {
"nop": noop,
"acc": acc,
"jmp": jmp
}
callstack = []
while True:
callstack.append(position)
op, arg = op_codes[position]
try:
op_map[op](int(arg))
except LoopException:
print(f"Value in the accumulator on loop detection: {accumulator}")
break
#%% Part 2
position_to_check = [x for x in callstack[:-1] if op_codes[x][0] in ["nop", "jmp"]]
terminated = False
for corruption_candidate in position_to_check:
if terminated:
break
position = 0
visited = set()
accumulator = 0
altered_op_codes = op_codes.copy()
old_op, arg = altered_op_codes[corruption_candidate]
altered_op_codes[corruption_candidate] = ("nop", arg) if old_op == "jmp" else ("jmp", arg)
while True:
if position >= len(altered_op_codes):
print(f"Value in the accumulator on termination: {accumulator}")
terminated = True
break
op, arg = altered_op_codes[position]
try:
op_map[op](int(arg))
except LoopException:
break