-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday17.py
240 lines (172 loc) · 4.38 KB
/
day17.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import sys
inputfile = ""
if len(sys.argv) == 2:
inputfile = sys.argv[1] + ".txt"
else:
inputfile = "input" + sys.argv[0][3:5] + ".txt"
class Register:
def __init__(self, val):
self.val = val
def get(self):
return self.val
def set(self,val):
self.val = val
def __str__(self):
return "Value: "+str(self.val)
with open(inputfile,"r") as input:
for line in input:
line = line.rstrip()
if line.startswith("Register A"):
A = Register(int(line[12:]))
elif line.startswith("Register B"):
B = Register(int(line[12:]))
b_init = int(line[12:])
elif line.startswith("Register C"):
C = Register(int(line[12:]))
c_init = int(line[12:])
elif line.startswith("Program"):
program = [int(x) for x in line[9:].split(",")]
def get_combo_op(op):
global A, B, C
if op <= 3:
return op
elif op == 4:
return A.get()
elif op == 5:
return B.get()
elif op == 6:
return C.get()
else:
print("huh?")
def div(register,op):
global A,debug
combo_op = get_combo_op(op)
numerator = A.get()
denominator = pow(2,combo_op)
floatresult = numerator / denominator
trunc = int(str(floatresult).split(".")[0])
if debug:
print("xdiv "+str(op)+" (resolves to "+str(combo_op)+") => "+str(trunc))
register.set(trunc)
def bxl(op):
global B
result = B.get() ^ op
if debug:
print("bxl "+str(op) +" => "+str(result))
B.set(result)
def bst(op):
global B,pointer
combo_op = get_combo_op(op)
result = combo_op % 8
if debug:
print("bst "+str(op)+" (resolves to "+str(combo_op)+") => "+str(result))
B.set(result)
def jnz(op):
global A, pointer
if A.get() == 0:
if debug:
print("jnz "+str(op)+" (not jumping)")
return pointer + 2
else:
if debug:
print("jnz "+str(op)+" (jumping)")
return op
def bxc(op):
global B, C
b = B.get()
c = C.get()
result = b ^ c
if debug:
print("bxc "+str(b) +" "+str(c)+" => "+str(result))
B.set(result)
def out(op):
combo_op = get_combo_op(op)
result = combo_op % 8
if debug:
print("out "+str(op)+" (resolves to "+str(combo_op)+") => "+str(result))
return result
def run():
global program, pointer
output = []
while pointer < len(program):
if debug:
print("Pointer: "+str(pointer)+" ("+str(program[pointer])+")")
if program[pointer] == 0:
div(A,program[pointer+1])
pointer += 2
elif program[pointer] == 1:
bxl(program[pointer+1])
pointer += 2
elif program[pointer] == 2:
bst(program[pointer+1])
pointer += 2
elif program[pointer] == 3:
pointer = jnz(program[pointer+1])
elif program[pointer] == 4:
bxc(program[pointer+1])
pointer += 2
elif program[pointer] == 5:
output.append(out(program[pointer+1]))
pointer += 2
elif program[pointer] == 6:
div(B,program[pointer+1])
pointer += 2
elif program[pointer] == 7:
div(C,program[pointer+1])
pointer += 2
else:
print("huh?!")
return output
debug = False
pointer = 0
def run_part1():
global pointer, debug
output = run()
print(A)
print(B)
print(C)
print(output)
print(",".join([str(x) for x in output]))
def run_part2():
global A,B,C,b_init,C_init,program,pointer
outputstr = ""
programstr = "5,5,3,0"#",".join([str(x) for x in program])
i = -1
while not outputstr == programstr:
i += 1
A.set(i)
B.set(b_init)
C.set(c_init)
pointer = 0
output = run()
outputstr = ",".join([str(x) for x in output])
if i % 100000 == 0:
print(i)
prefix = 11
if outputstr[:prefix] == programstr[:prefix]:
print("Found something for "+str(i)+": "+outputstr)
print(i)
def run_part2_2():
global A,B,C,b_init,C_init,program,pointer
startwith = 56
instructions = program.copy()
lookfor = str(instructions.pop(-2))+","+str(instructions.pop(-1))
outputstr = ""
while True:
while not outputstr == lookfor:
A.set(startwith)
B.set(b_init)
C.set(c_init)
pointer = 0
output = run()
outputstr = ",".join([str(x) for x in output])
startwith += 1
startwith += -1
print(str(startwith)+" => "+outputstr)
if len(output) == len(program):
break
else:
lookfor = str(instructions.pop(-1))+","+lookfor
startwith = startwith * 8
run_part1()
run_part2_2()