-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday8.py
51 lines (38 loc) · 1.23 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
from common_py import input_access
input = input_access.fetch_input(8)
commands = list(map(lambda s : s.split(" "), input.rstrip().split("\n")))
def executeCode(instructions):
# print(commands)
pointer, globalValue = 0, 0
visitedInstructions = set()
while pointer < len(instructions):
if pointer in visitedInstructions:
raise IndexError(globalValue)
visitedInstructions.add(pointer)
instruction = instructions[pointer][0]
value = int(instructions[pointer][1])
if instruction == "nop":
pointer = pointer + 1
elif instruction == "acc":
globalValue += value
pointer = pointer + 1
elif instruction == "jmp":
pointer = pointer + value
return globalValue
try:
executeCode(commands)
except IndexError as e:
print(e)
# Part2
nop_jmp_indices = []
for i in range(len(commands)):
if commands[i][0] == "nop" or commands[i][0] == "jmp":
nop_jmp_indices.append(i)
for index in nop_jmp_indices:
modified_commands = list(map(lambda listitem : list(listitem), commands))
modified_commands[index][0] = "jmp" if modified_commands[index][0] == "nop" else "nop"
try:
result = executeCode(modified_commands)
print("kiralysag", result)
except IndexError as e:
continue