-
Notifications
You must be signed in to change notification settings - Fork 0
/
day8.py
66 lines (53 loc) · 1.55 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
61
62
63
64
65
66
with open("input/day8", "r") as input:
data = input.read().split("\n")
data.pop()
def part1():
acc = 0
idx = 0
cache = {}
while True:
if idx in cache:
break
entry = data[idx].split(" ")
cache[idx] = (entry[0], int(entry[1]))
if cache[idx][0] == "acc":
acc += cache[idx][1]
idx += 1
elif cache[idx][0] == "jmp":
idx += cache[idx][1]
else: #nop
idx += 1
return acc
def part2():
idxNextChange = 0
while idxNextChange < len(data):
acc = 0
idx = 0
idxInstructionsPassed = 0
cache = {}
while True:
if idx in cache or idx >= len(data):
idxNextChange += 1
break
entry = data[idx].split(" ")
cache[idx] = (entry[0], int(entry[1]))
if cache[idx][0] == "acc":
acc += cache[idx][1]
idx += 1
elif cache[idx][0] == "jmp":
if idxNextChange == idxInstructionsPassed:
idx += 1
else:
idx += cache[idx][1]
idxInstructionsPassed += 1
else: #nop
if idxNextChange == idxInstructionsPassed:
idx += cache[idx][1]
else:
idx += 1
idxInstructionsPassed += 1
if idx == len(data):
break
return acc
print("Part 1: " + str(part1()))
print("Part 2: " + str(part2()))