-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay8.py
59 lines (49 loc) · 1.33 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
# Immediately before any instruction is executed a second time, what value is in the accumulator?
instructions = []
while True:
line = input()
if line == "end":
break
line = line.split(" ")
instructions.append((line[0], int(line[1])))
def run():
visited = [0] * len(instructions)
pos = 0
acc = 0
halted = False
while pos < len(instructions):
# keeps track of executed instructions
if visited[pos] == 1:
halted = True
break
else:
visited[pos] = 1
# executes the instructions
if instructions[pos][0] == "jmp":
pos += instructions[pos][1]
elif instructions[pos][0] == "nop":
pos += 1
else:
acc += instructions[pos][1]
pos += 1
return acc, halted
def part_one():
print(run())
def part_two():
pos = 0
while True:
(instr, step) = instructions[pos]
if instr == "jmp":
instructions[pos] = ("nop", step)
elif instr == "nop":
instructions[pos] = ("jmp", step)
else:
pos += 1
continue
(acc, halted) = run()
if halted:
instructions[pos] = (instr, step)
pos += step if instr == "jmp" else 1
else:
print(acc)
break