-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08.py
66 lines (52 loc) · 1.39 KB
/
08.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
# part one
accumulator = 0
commands = []
position = 0
known = []
f = open("08.txt", "r")
for l in f:
commands.append(l.rstrip())
f.close()
while position not in known:
known.append(position)
command = commands[position]
if command[:3] == "jmp":
position = position + int(command[4:])
elif command[:3] == "acc":
accumulator = accumulator + int(command[4:])
position += 1
elif command[:3] == "nop":
position += 1
print(accumulator)
# part two
commands = []
comfix = 0
while(True):
known = []
commands = []
f = open("08.txt", "r")
for l in f:
commands.append(l.rstrip())
f.close()
position = 0
accumulator = 0
if comfix == len(commands):
break
if commands[comfix][:3] == "jmp":
commands[comfix] = "nop" + commands[comfix][3:]
elif commands[comfix][:3] == "nop":
commands[comfix] = "jmp" + commands[comfix][3:]
while position not in known:
known.append(position)
if(position < len(commands)):
command = commands[position]
if command[:3] == "jmp":
position += int(command[4:])
elif command[:3] == "acc":
accumulator += int(command[4:])
position += 1
elif command[:3] == "nop":
position += 1
else:
print(accumulator)
comfix += 1