-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD7-2.py
331 lines (325 loc) · 14.1 KB
/
D7-2.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# all possibilities for phases set
def phasecreator(nums):
from itertools import permutations
phasepossibilities=[]
for value in permutations(nums, len(nums)):
phasepossibilities.append(list(value))
return phasepossibilities
def memory(Intcode,phase,outputs,opcode3,pointer):
NewIncode=[]
# print("Input",Intcode)
for item in Intcode:
if int(item)<0:
NewIncode.append(int(item))
else:
if len(str(item))==1:
new_item='000'+str(item)
NewIncode.append(new_item)
elif len(str(item))==2:
new_item='00'+str(item)
NewIncode.append(new_item)
elif len(str(item))==3:
new_item='0'+str(item)
NewIncode.append(new_item)
else:
NewIncode.append(item)
NewIncode.append(0) # add one '0' at the end to support the system pointer
# print("Revised digits",NewIncode, len(NewIncode))
eachRun={'opcode3':opcode3,'status':0}
while True:
instruction=NewIncode[pointer]
if int(instruction)!=99:
# print('Pointer#',pointer)
parameters1=int(NewIncode[pointer+1])
parameters2=int(NewIncode[pointer+2])
parameters3=int(NewIncode[pointer+3])
forH=int(str(instruction)[-3])
forT=int(str(instruction)[-4])
if forH==0 and forT==0:
if str(instruction)[-1]=='1':
add4=int(NewIncode[parameters1])+int(NewIncode[parameters2])
NewIncode[parameters3]=int(add4)
pointer+=4
elif str(instruction)[-1]=='2':
multiply4=int(NewIncode[parameters1])*int(NewIncode[parameters2])
NewIncode[parameters3]=int(multiply4)
pointer+=4
elif str(instruction)[-1]=='3':
if str(instruction)[-1]=='3' and eachRun['opcode3']==0:
ACuser=int(phase)
eachRun['opcode3']=1
# ACuser=input('enter phase: ')
parameters1=int(NewIncode[pointer+1])
NewIncode[parameters1]=int(ACuser)
# print('1st 3',NewIncode)
pointer+=2
elif str(instruction)[-1]=='3'and eachRun['opcode3']==1:
ACuser=int(outputs)
eachRun['opcode3']=2
# ACuser=input('enter input: ')
parameters1=int(NewIncode[pointer+1])
NewIncode[parameters1]=int(ACuser)
# print('2nd 3',NewIncode)
pointer+=2
elif str(instruction)[-1]=='3'and eachRun['opcode3']==2:
pointerlater=pointer
amplifier=NewIncode
break
elif str(instruction)[-1]=='4':
# print('opcode4-0 pre-pointer',pointer)
printout=int(NewIncode[parameters1]) #forH=0
pointer+=2
# print('opcode4-0 post-pointer',pointer)
elif str(instruction)[-1]=='5':
if int(NewIncode[parameters1]) is 0:
pointer+=3
else:
pointer+=3
pointer=int(NewIncode[parameters2])
if pointer>len(Intcode):
print('something too big1')
break
elif str(instruction)[-1]=='6':
if int(NewIncode[parameters1]) is 0:
pointer=int(NewIncode[parameters2])
if pointer>len(Intcode):
print('something too big5')
break
else:
pointer+=3
elif str(instruction)[-1]=='7':
if int(NewIncode[parameters1])<int(NewIncode[parameters2]):
NewIncode[parameters3]=1
else:
NewIncode[parameters3]=0
pointer+=4
elif str(instruction)[-1]=='8':
if int(NewIncode[parameters1])==int(NewIncode[parameters2]):
NewIncode[parameters3]=1
else:
NewIncode[parameters3]=0
pointer+=4
elif forH==1 and forT==0:
if str(instruction)[-1]=='1':
add4=parameters1+int(NewIncode[parameters2])
NewIncode[parameters3]=int(add4)
pointer+=4
elif str(instruction)[-1]=='2':
multiply4=parameters1*int(NewIncode[parameters2])
NewIncode[parameters3]=int(multiply4)
pointer+=4
elif str(instruction)[-1]=='3':
if str(instruction)[-1]=='3' and eachRun['opcode3']==0:
ACuser=int(phase)
# ACuser=input('enter phase: ')
eachRun['opcode3']=1
parameters1=int(NewIncode[pointer+1])
NewIncode[parameters1]=int(ACuser)
pointer+=2
elif str(instruction)[-1]=='3'and eachRun['opcode3']==1:
ACuser=int(outputs)
# ACuser=input('enter input: ')
eachRun['opcode3']=2
parameters1=int(NewIncode[pointer+1])
NewIncode[parameters1]=int(ACuser)
pointer+=2
elif str(instruction)[-1]=='3'and eachRun['opcode3']==2:
pointerlater=pointer
amplifier=NewIncode
break
elif str(instruction)[-1]=='4':
# print('opcode4-1 pre-pointer',pointer)
printout=int(parameters1) #forH=1
pointer+=2
# print('opcode4-1 post-pointer',pointer)
elif str(instruction)[-1]=='5':
if parameters1 is 0:
pointer+=3
else:
pointer=int(NewIncode[parameters2])
if pointer>len(Intcode):
print('something too big2')
break
elif str(instruction)[-1]=='6':
if parameters1 is 0:
pointer=int(NewIncode[parameters2])
if pointer>len(Intcode):
print('something too big6')
break
else:
pointer+=3
elif str(instruction)[-1]=='7':
if parameters1<int(NewIncode[parameters2]):
NewIncode[parameters3]=1
else:
NewIncode[parameters3]=0
pointer+=4
elif str(instruction)[-1]=='8':
if parameters1==int(NewIncode[parameters2]):
NewIncode[parameters3]=1
else:
NewIncode[parameters3]=0
pointer+=4
elif forH==0 and forT==1:
if str(instruction)[-1]=='1':
add4=int(NewIncode[parameters1])+parameters2
NewIncode[parameters3]=int(add4)
pointer+=4
elif str(instruction)[-1]=='2':
multiply4=int(NewIncode[parameters1])*parameters2
NewIncode[parameters3]=int(multiply4)
pointer+=4
elif str(instruction)[-1]=='5':
if int(NewIncode[parameters1]) is 0:
pointer+=3
else:
pointer=parameters2
if pointer>len(Intcode):
print('something too big3')
break
elif str(instruction)[-1]=='6':
if int(NewIncode[parameters1]) is 0:
pointer=parameters2
if pointer>len(Intcode):
print('something too big7')
break
else:
pointer+=3
elif str(instruction)[-1]=='7':
if int(NewIncode[parameters1])<parameters2:
NewIncode[parameters3]=1
else:
NewIncode[parameters3]=0
pointer+=4
elif str(instruction)[-1]=='8':
if int(NewIncode[parameters1])==parameters2:
NewIncode[parameters3]=1
else:
NewIncode[parameters3]=0
pointer+=4
elif forH==1 and forT==1:
if str(instruction)[-1]=='1':
add4=parameters1+parameters2
NewIncode[parameters3]=int(add4)
pointer+=4
elif str(instruction)[-1]=='2':
multiply4=parameters1*parameters2
NewIncode[parameters3]=int(multiply4)
pointer+=4
elif str(instruction)[-1]=='5':
if parameters1 is 0:
pointer+=3
else:
pointer=parameters2
if pointer>len(Intcode):
print('something too big4')
break
elif str(instruction)[-1]=='6':
if parameters1 is 0:
pointer=parameters2
if pointer>len(Intcode):
print('something too big8')
break
else:
pointer+=3
elif str(instruction)[-1]=='7':
if parameters1<parameters2:
NewIncode[parameters3]=1
else:
NewIncode[parameters3]=0
pointer+=4
elif str(instruction)[-1]=='8':
if parameters1==parameters2:
NewIncode[parameters3]=1
else:
NewIncode[parameters3]=0
pointer+=4
elif int(instruction)==99:
# print('Stop for 99 at',pointer,'\n')
eachRun['status']=1
amplifier='stop'
pointerlater='stop'
break
else:
print('Something went wrong.')
break
# print('The pointer becomes',pointerlater)
# print('One round ended.')
return amplifier,printout,pointerlater,eachRun['status']
with open('D7-data.txt','r') as intcode:
int_data=intcode.read()
upgradeAC=list(int_data.split(','))
# print('Length of AC',len(upgradeAC))
phase_list=[5,6,7,8,9]
signals=[]
confirmset=0
for phaseset in phasecreator(phase_list):
# print("Phase set",phaseset)
repeat=0
inputA=[0]
inputB=[]
inputC=[]
inputD=[]
inputE=[]
while repeat==0 or repeat==1:
if repeat==0:
# print('Repeat#',repeat)
# print('Input A',inputA[-1])
amplifierA,outputA,pointerA,statusA=memory(upgradeAC,phaseset[0],inputA[-1],0,0)
inputB.append(outputA)
# print(inputB[-1])
amplifierB,outputB,pointerB,statusB=memory(upgradeAC,phaseset[1],inputB[-1],0,0)
inputC.append(outputB)
# print(inputC[-1])
amplifierC,outputC,pointerC,statusC=memory(upgradeAC,phaseset[2],inputC[-1],0,0)
inputD.append(outputC)
# print(inputD[-1])
amplifierD,outputD,pointerD,statusD=memory(upgradeAC,phaseset[3],inputD[-1],0,0)
inputE.append(outputD)
# print(inputE[-1])
amplifierE,outputE,pointerE,statusE=memory(upgradeAC,phaseset[4],inputE[-1],0,0)
inputA.append(outputE)
# print('OutputE',outputE)
repeat=1
else:
# print('Repeat#',repeat)
# print('Input A',inputA[-1])
amplifierA1,outputA,pointerA,statusA=memory(amplifierA,phaseset[0],inputA[-1],1,pointerA)
# print('StatusA changes',statusA)
inputB.append(outputA)
# print(inputB[-1])
amplifierB1,outputB,pointerB,statusB=memory(amplifierB,phaseset[1],inputB[-1],1,pointerB)
# print('StatusB changes',statusB)
inputC.append(outputB)
# print(inputC[-1])
amplifierC1,outputC,pointerC,statusC=memory(amplifierC,phaseset[2],inputC[-1],1,pointerC)
# print('StatusC changes',statusC)
inputD.append(outputC)
# print(inputD[-1])
amplifierD1,outputD,pointerD,statusD=memory(amplifierD,phaseset[3],inputD[-1],1,pointerD)
# print('StatusD changes',statusD)
inputE.append(outputD)
# print(inputE[-1])
amplifierE1,outputE,pointerE,statusE=memory(amplifierE,phaseset[4],inputE[-1],1,pointerE)
# print('StatusE changes',statusE)
inputA.append(outputE)
# print('OutputE',outputE)
# Re-name the amplifier to trigger the next round
amplifierA=amplifierA1
amplifierB=amplifierB1
amplifierC=amplifierC1
amplifierD=amplifierD1
amplifierE=amplifierE1
if statusA==1 and statusB==1 and statusC==1 and statusD==1 and statusE==1:
repeat=2
# print('Repeat#',repeat)
# print('Final outputE',outputE)
signals.append(outputE)
break
# else:
# print('Something wrong.')
confirmset+=1
# print('Total number of set',confirmset)
print("Signals collection",signals)
print('The highest signal is',max(signals))
intcode.close()