-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8.py
68 lines (56 loc) · 1.65 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
67
68
def day8p1(inp):
command_list = inp.split('\n')
seen = [False] * len(command_list)
i = 0
total = 0
while not seen[i]:
cmd, val = command_list[i].split()
seen[i] = True
if cmd == 'acc':
total += int(val)
i += 1
elif cmd == 'jmp':
i += int(val)
else:
i += 1
return total
def day8p2(inp):
command_list = inp.split('\n')
seen = [False] * len(command_list)
i = 0
total = 0
while not seen[i]:
cmd, val = command_list[i].split()
seen[i] = True
if cmd == 'acc':
total += int(val)
i += 1
elif cmd == 'jmp':
i += int(val)
else:
i += 1
for j, s in enumerate(seen):
if s:
cmd, val = command_list[j].split()
inv_dic = {'jmp': 'nop', 'nop': 'jmp'}
if cmd in inv_dic:
i = 0
total = 0
seen2 = [False] * len(command_list)
while not seen2[i]:
cmd, val = command_list[i].split()
if i == j:
cmd = inv_dic[cmd]
seen2[i] = True
if cmd == 'acc':
total += int(val)
i += 1
elif cmd == 'jmp':
i += int(val)
else:
i += 1
if i >= len(command_list):
return total
return -1
# print(day8p1(inp1))
# print(day8p2(inp1))