-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday7.py
268 lines (238 loc) · 8.75 KB
/
day7.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
#AOC 2019 Brianna Frye
#Day 7
# Opcodes:
# 1 - add, 3 params: num, num, position to store at
# XXX01
# 2 - multiply, 3 params: num, num, position to store at
# XXX02
# 3 - input, 1 param: position to store
# X03
# 4 - output, 1 param: value/address to output
# X04
# 5 - jump if true, 2 params: condition, jump location
# 6 - jump if false, 2 params: condition, jump location
# 7 - less than, 3 params: base, comparator, storage location
# 8 - equals, 3 params: num, num, storage location
# 99 - halt, no params
# 99
# 0 = position mode
# 1 = immediate mode
from itertools import permutations
def parse_to_array(base_file):
data_left = True
output_array = []
try:
file = open(base_file)
while data_left == True:
temp = file.readline()
if temp == '':
data_left = False
break
output_array = temp.split(',')
file.close()
return output_array
except IOError:
print('File does not exist!')
finally:
print('Parsing complete!')
def intcode_comp(data, phase, amp_input):
modes_specified = False
entered_phase = False
i = 0
while(True):
opcode = int(data[i])
modes = ''
if len(str(opcode)) > 1:
command = int(str(opcode)[-2:])
modes_specified = True
modes = str(opcode)[:-2]
else:
command = opcode
modes_specified = False
# print('opcode: ' + str(opcode))
# storage = int(data[i + 3])
# print('storage: ' + str(storage))
if command == 1:
if modes_specified:
#add back leading zeroes
while len(modes) < 3:
modes = '0' + modes
if int(modes[-1]) == 1:
first_addend = int(data[i + 1])
else:
first_addend = int(data[int(data[i + 1])])
if int(modes[-2]) == 1:
second_addend = int(data[i + 2])
else:
second_addend = int(data[int(data[i + 2])])
storage_location = int(data[i + 3])
else:
first_addend = int(data[int(data[i + 1])])
second_addend = int(data[int(data[i + 2])])
storage_location = int(data[i + 3])
# print('adding ' + str(first_addend) + ' and ' + str(second_addend) + ', storing at ' + str(storage_location))
result = first_addend + second_addend
data[storage_location] = result
i += 4
elif command == 2:
if modes_specified:
#add back leading zeroes
while len(modes) < 3:
modes = '0' + modes
if int(modes[-1]) == 1:
first_multiplicand = int(data[i + 1])
else:
first_multiplicand = int(data[int(data[i + 1])])
if int(modes[-2]) == 1:
second_multiplicand = int(data[i + 2])
else:
second_multiplicand = int(data[int(data[i + 2])])
storage_location = int(data[i + 3])
else:
first_multiplicand = int(data[int(data[i + 1])])
second_multiplicand = int(data[int(data[i + 2])])
storage_location = int(data[i + 3])
result = first_multiplicand * second_multiplicand
data[storage_location] = result
i += 4
elif command == 3:
# inp = int(input('HIT AN INPUT COMMAND. PLEASE GIVE INPUT: '))
# print(str(inp) + ' WAS THE INPUT')
if entered_phase == True:
inp = amp_input
else:
inp = phase
entered_phase = True
if modes_specified:
if int(modes[0]) == 1:
storage_location = int(data[i + 1])
else:
print('YOU MESSED UP SOMEWHERE')
else:
storage_location = int(data[i + 1])
data[storage_location] = inp
i += 2
elif command == 4:
if modes_specified:
if int(modes[0]) == 1:
print('OUTPUT: ' + str(data[i + 1]))
return str(data[i + 1])
print('OUTPUT: ' + str(data[int(data[i + 1])]))
break
else:
print('YOU MESSED UP SOMEWHERE')
else:
print('OUTPUT: ' + str(data[int(data[i + 1])]))
return str(data[int(data[i + 1])])
i += 2
elif command == 5:
if modes_specified:
#add back leading zeroes
while len(modes) < 2:
modes = '0' + modes
if int(modes[-1]) == 1:
test_num = int(data[i + 1])
else:
test_num = int(data[int(data[i + 1])])
if int(modes[-2]) == 1:
new_pointer = int(data[i + 2])
else:
new_pointer = int(data[int(data[i + 2])])
else:
test_num = int(data[int(data[i + 1])])
new_pointer = int(data[i + 2])
new_pointer = int(data[int(data[i + 2])])
if test_num != 0:
i = new_pointer
else:
i += 3
elif command == 6:
if modes_specified:
#add back leading zeroes
while len(modes) < 2:
modes = '0' + modes
if int(modes[-1]) == 1:
test_num = int(data[i + 1])
else:
test_num = int(data[int(data[i + 1])])
if int(modes[-2]) == 1:
new_pointer = int(data[i + 2])
else:
new_pointer = int(data[int(data[i + 2])])
else:
test_num = int(data[int(data[i + 1])])
new_pointer = int(data[i + 2])
new_pointer = int(data[int(data[i + 2])])
if test_num == 0:
i = new_pointer
else:
i += 3
elif command == 7:
if modes_specified:
#add back leading zeroes
while len(modes) < 3:
modes = '0' + modes
if int(modes[-1]) == 1:
first_num = int(data[i + 1])
else:
first_num = int(data[int(data[i + 1])])
if int(modes[-2]) == 1:
second_num = int(data[i + 2])
else:
second_num = int(data[int(data[i + 2])])
storage_location = int(data[i + 3])
else:
first_num = int(data[int(data[i + 1])])
second_num = int(data[int(data[i + 2])])
storage_location = int(data[i + 3])
if first_num < second_num:
data[storage_location] = 1
else:
data[storage_location] = 0
i += 4
elif command == 8:
if modes_specified:
#add back leading zeroes
while len(modes) < 3:
modes = '0' + modes
if int(modes[-1]) == 1:
first_num = int(data[i + 1])
else:
first_num = int(data[int(data[i + 1])])
if int(modes[-2]) == 1:
second_num = int(data[i + 2])
else:
second_num = int(data[int(data[i + 2])])
storage_location = int(data[i + 3])
else:
first_num = int(data[int(data[i + 1])])
second_num = int(data[int(data[i + 2])])
storage_location = int(data[i + 3])
if first_num == second_num:
data[storage_location] = 1
else:
data[storage_location] = 0
i += 4
elif command == 99:
print('stopping!')
break
else:
print('YA DONE GOOFED')
break
# print(data)
def get_thruster_signal(levels, program):
amp_a_out = intcode_comp(program, levels[0], 0)
amp_b_out = intcode_comp(program, levels[1], amp_a_out)
amp_c_out = intcode_comp(program, levels[2], amp_b_out)
amp_d_out = intcode_comp(program, levels[3], amp_c_out)
amp_e_out = intcode_comp(program, levels[4], amp_d_out)
return int(amp_e_out)
data = parse_to_array('day7input.txt')
level_source = [0, 1, 2, 3, 4]
levels = permutations(level_source)
max_signal = 0
for i in levels:
temp = get_thruster_signal(i, data)
if temp > max_signal:
max_signal = temp
print(max_signal)