-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday8.py
More file actions
80 lines (70 loc) · 2.07 KB
/
day8.py
File metadata and controls
80 lines (70 loc) · 2.07 KB
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
69
70
71
72
73
74
75
76
77
78
79
instructions = open('day8-input.txt').readlines()
def brokenAcc():
instructions_ran = set()
acc = 0
idx = 0
while idx in range(len(instructions)):
if idx in instructions_ran:
return acc
else:
ins = instructions[idx]
instructions_ran.add(idx)
op, num = ins.split(' ')
if op == 'acc':
acc += int(num)
idx += 1
elif op == 'jmp':
idx += int(num)
else:
idx += 1
return 'Should never get here'
print(brokenAcc())
# Fix the program by changing exactly one jmp to nop or nop to jmp
# Find all the fixable instructions, and fix them to see if the program terminates
# returns the acc if it reaches the end, else NONE
def acc(modified_instructions):
instructions_ran = []
acc = 0
idx = 0
while idx in range(len(modified_instructions)):
if idx in instructions_ran:
return None
else:
ins = modified_instructions[idx]
instructions_ran.append(idx)
op, num = ins.split(' ')
if op == 'acc':
acc += int(num)
idx += 1
elif op == 'jmp':
idx += int(num)
else:
idx += 1
if idx == len(instructions):
return acc
else:
return None
def findFixableLines():
fixable = []
for idx in range(len(instructions)):
op, num = instructions[idx].split(' ')
if op != 'acc':
fixable.append(idx)
return fixable
def fixProgram():
candidates = findFixableLines()
for idx in candidates:
op, num = instructions[idx].split(' ')
new_op = None
if op == 'jmp':
new_op = 'nop'
else:
new_op = 'jmp'
modified_inst = instructions[0:idx]
modified_inst.append(new_op + ' ' + num)
modified_inst += instructions[(idx+1):]
res = acc(modified_inst)
if res != None:
return res
return 'Found nothing'
print(fixProgram())