-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpuzzle8_2.py
More file actions
94 lines (78 loc) · 1.82 KB
/
Copy pathpuzzle8_2.py
File metadata and controls
94 lines (78 loc) · 1.82 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
80
81
82
83
84
85
86
87
88
89
90
91
import numpy as np
import sys
"""
nop +0 | 1
acc +1 | 2
jmp +4 | 3
acc +3 |
jmp -3 |
acc -99 |
acc +1 | 4
nop -4 | 5
acc +6 | 6
Fix the program so that it terminates normally by changing exactly one jmp (to nop) or nop (to jmp).
"""
def check_lines(lines):
N = len(lines)
line_nrs = []
accumulator = 0
loop = False
i=0
while not loop:
line = lines[i].strip()
instruction = line.split()[0]
value = line.split()[1]
if i in line_nrs:
print("Looping!",instruction,value,'accumulator=',accumulator)
loop = True
break
line_nrs.append(i)
if instruction == 'acc':
accumulator += int(value)
print(instruction,value,'acc=',accumulator)
i+=1
elif instruction == 'jmp':
print(instruction,value,'acc=',accumulator)
i+=int(value)
elif instruction == 'nop':
print(instruction,value,'acc=',accumulator)
i+=1
if i == N:
print("DONE")
break
return accumulator,loop
with open('day8.in','r') as infile:
lines = infile.readlines()
instr = []
vals = []
for l in lines:
instr.append(l.split()[0].strip())
vals.append(l.split()[1].strip())
instr = np.array(instr)
vals = np.array(vals)
ind_nop = np.argwhere(instr=='nop').flatten()
ind_jmp = np.argwhere(instr=='jmp').flatten()
print(ind_nop,ind_jmp)
counter = 0
loop = True
while loop:
for k in ind_jmp:
lines_copy = np.copy(lines)
lines_copy[k] = 'nop '+vals[k]
print("---change",lines[k].strip(),"-->",lines_copy[k],k)
accumulator,loop = check_lines(lines_copy)
if loop==False:
break
counter += 1
# Flaks for glemte å kommentere inn dette!!!
#for k in ind_nop:
# lines_copy = np.copy(lines)
# lines_copy[k] = 'jmp '+vals[k]
# print("---change",lines[k].strip(),"-->",lines_copy[k])
# accumulator,loop = check_lines(lines_copy)
# if loop==False:
# break
# counter += 1
if counter >3:
break
print(accumulator)