-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
334 lines (288 loc) · 18.8 KB
/
main.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
332
333
334
import logging
from collections import deque
from itertools import cycle, permutations
from computer import IntcodeComputer
from helpers import setup_logger
##############################################
# Driver Functions
##############################################
def run_day2_test():
"""
Solve the test provided in Advent of Code 2019 day 2 part 1.
Runs the simple test provided in day 2 part 1, and prints the result to the console. The result should be 3500.
:return: None
"""
program = [1, 9, 10, 3, 2, 3, 11, 0, 99, 30, 40, 50]
computer = IntcodeComputer(program, 1)
setup_logger('Computer 1', 'comp1.log')
computer.run(deque())
print(computer.program[0])
def run_day5_part1():
"""
Solve Advent of Code 2019 day 5 part 1.
Runs the day 5 program with the initial input value of 1, and prints the result to the console.
:return: None
"""
output: int = 0
program = [3, 225, 1, 225, 6, 6, 1100, 1, 238, 225, 104, 0, 2, 106, 196, 224, 101, -1157, 224, 224, 4, 224, 102, 8,
223, 223, 1001, 224, 7, 224, 1, 224, 223, 223, 1002, 144, 30, 224, 1001, 224, -1710, 224, 4, 224, 1002,
223, 8, 223, 101, 1, 224, 224, 1, 224, 223, 223, 101, 82, 109, 224, 1001, 224, -111, 224, 4, 224, 102, 8,
223, 223, 1001, 224, 4, 224, 1, 223, 224, 223, 1102, 10, 50, 225, 1102, 48, 24, 224, 1001, 224, -1152,
224, 4, 224, 1002, 223, 8, 223, 101, 5, 224, 224, 1, 223, 224, 223, 1102, 44, 89, 225, 1101, 29, 74, 225,
1101, 13, 59, 225, 1101, 49, 60, 225, 1101, 89, 71, 224, 1001, 224, -160, 224, 4, 224, 1002, 223, 8, 223,
1001, 224, 6, 224, 1, 223, 224, 223, 1101, 27, 57, 225, 102, 23, 114, 224, 1001, 224, -1357, 224, 4, 224,
102, 8, 223, 223, 101, 5, 224, 224, 1, 224, 223, 223, 1001, 192, 49, 224, 1001, 224, -121, 224, 4, 224,
1002, 223, 8, 223, 101, 3, 224, 224, 1, 223, 224, 223, 1102, 81, 72, 225, 1102, 12, 13, 225, 1, 80, 118,
224, 1001, 224, -110, 224, 4, 224, 102, 8, 223, 223, 101, 2, 224, 224, 1, 224, 223, 223, 4, 223, 99, 0,
0, 0, 677, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1105, 0, 99999, 1105, 227, 247, 1105, 1, 99999, 1005, 227,
99999, 1005, 0, 256, 1105, 1, 99999, 1106, 227, 99999, 1106, 0, 265, 1105, 1, 99999, 1006, 0, 99999,
1006, 227, 274, 1105, 1, 99999, 1105, 1, 280, 1105, 1, 99999, 1, 225, 225, 225, 1101, 294, 0, 0, 105, 1,
0, 1105, 1, 99999, 1106, 0, 300, 1105, 1, 99999, 1, 225, 225, 225, 1101, 314, 0, 0, 106, 0, 0, 1105, 1,
99999, 7, 677, 226, 224, 102, 2, 223, 223, 1005, 224, 329, 101, 1, 223, 223, 108, 226, 226, 224, 102, 2,
223, 223, 1006, 224, 344, 101, 1, 223, 223, 1108, 226, 677, 224, 102, 2, 223, 223, 1006, 224, 359, 1001,
223, 1, 223, 107, 677, 677, 224, 1002, 223, 2, 223, 1005, 224, 374, 1001, 223, 1, 223, 1107, 226, 677,
224, 102, 2, 223, 223, 1005, 224, 389, 1001, 223, 1, 223, 107, 677, 226, 224, 1002, 223, 2, 223, 1005,
224, 404, 101, 1, 223, 223, 8, 226, 677, 224, 102, 2, 223, 223, 1005, 224, 419, 101, 1, 223, 223, 7, 226,
677, 224, 1002, 223, 2, 223, 1005, 224, 434, 101, 1, 223, 223, 1007, 677, 677, 224, 102, 2, 223, 223,
1006, 224, 449, 1001, 223, 1, 223, 107, 226, 226, 224, 1002, 223, 2, 223, 1006, 224, 464, 1001, 223, 1,
223, 1007, 226, 226, 224, 102, 2, 223, 223, 1006, 224, 479, 1001, 223, 1, 223, 1008, 226, 226, 224, 102,
2, 223, 223, 1006, 224, 494, 101, 1, 223, 223, 7, 677, 677, 224, 102, 2, 223, 223, 1005, 224, 509, 1001,
223, 1, 223, 108, 677, 226, 224, 102, 2, 223, 223, 1005, 224, 524, 101, 1, 223, 223, 1108, 677, 226, 224,
1002, 223, 2, 223, 1006, 224, 539, 101, 1, 223, 223, 1108, 677, 677, 224, 102, 2, 223, 223, 1005, 224,
554, 101, 1, 223, 223, 8, 677, 226, 224, 102, 2, 223, 223, 1005, 224, 569, 101, 1, 223, 223, 8, 677, 677,
224, 102, 2, 223, 223, 1005, 224, 584, 101, 1, 223, 223, 1107, 226, 226, 224, 102, 2, 223, 223, 1006,
224, 599, 101, 1, 223, 223, 108, 677, 677, 224, 102, 2, 223, 223, 1006, 224, 614, 101, 1, 223, 223, 1008,
677, 226, 224, 1002, 223, 2, 223, 1005, 224, 629, 1001, 223, 1, 223, 1107, 677, 226, 224, 102, 2, 223,
223, 1005, 224, 644, 101, 1, 223, 223, 1008, 677, 677, 224, 1002, 223, 2, 223, 1005, 224, 659, 101, 1,
223, 223, 1007, 677, 226, 224, 1002, 223, 2, 223, 1005, 224, 674, 1001, 223, 1, 223, 4, 223, 99, 226]
# Additional tests provided in the problem description for day 5.
test1 = [3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8]
test2 = [3, 9, 7, 9, 10, 9, 4, 9, 99, -1, 8]
test3 = [3, 3, 1108, -1, 8, 3, 4, 3, 99]
test4 = [3, 3, 1107, -1, 8, 3, 4, 3, 99]
test5 = [3, 12, 6, 12, 15, 1, 13, 14, 13, 4, 13, 99, -1, 0, 1, 9]
test6 = [3, 3, 1105, -1, 9, 1101, 0, 0, 12, 4, 12, 99, 1]
test7 = [3, 21, 1008, 21, 8, 20, 1005, 20, 22, 107, 8, 21, 20, 1006, 20, 31,
1106, 0, 36, 98, 0, 0, 1002, 21, 125, 20, 4, 20, 1105, 1, 46, 104,
999, 1105, 1, 46, 1101, 1000, 1, 20, 4, 20, 1105, 1, 46, 98, 99]
# program = test7 # Uncomment and modify this line to select one of the additional tests above.
computer = IntcodeComputer(program, 1)
setup_logger('Computer 1', 'comp1.log')
data = deque()
data.append(1)
while not computer.halted:
computer.run(data)
if not computer.halted:
output = data.pop()
print(f'Diagnostic program output is {output}')
def run_day5_part2():
"""
Solve Advent of Code 2019 day 5 part 2.
Runs the day 5 program with the initial input value of 5, and prints the result to the console.
:return: None
"""
output: int = 0
program = [3, 225, 1, 225, 6, 6, 1100, 1, 238, 225, 104, 0, 1102, 88, 66, 225, 101, 8, 125, 224, 101, -88, 224, 224,
4, 224, 1002, 223, 8, 223, 101, 2, 224, 224, 1, 224, 223, 223, 1101, 87, 23, 225, 1102, 17, 10, 224, 101,
-170, 224, 224, 4, 224, 102, 8, 223, 223, 101, 3, 224, 224, 1, 223, 224, 223, 1101, 9, 65, 225, 1101, 57,
74, 225, 1101, 66, 73, 225, 1101, 22, 37, 224, 101, -59, 224, 224, 4, 224, 102, 8, 223, 223, 1001, 224,
1, 224, 1, 223, 224, 223, 1102, 79, 64, 225, 1001, 130, 82, 224, 101, -113, 224, 224, 4, 224, 102, 8,
223, 223, 1001, 224, 7, 224, 1, 223, 224, 223, 1102, 80, 17, 225, 1101, 32, 31, 225, 1, 65, 40, 224,
1001, 224, -32, 224, 4, 224, 102, 8, 223, 223, 1001, 224, 4, 224, 1, 224, 223, 223, 2, 99, 69, 224, 1001,
224, -4503, 224, 4, 224, 102, 8, 223, 223, 101, 6, 224, 224, 1, 223, 224, 223, 1002, 14, 92, 224, 1001,
224, -6072, 224, 4, 224, 102, 8, 223, 223, 101, 5, 224, 224, 1, 223, 224, 223, 102, 33, 74, 224, 1001,
224, -2409, 224, 4, 224, 1002, 223, 8, 223, 101, 7, 224, 224, 1, 223, 224, 223, 4, 223, 99, 0, 0, 0, 677,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1105, 0, 99999, 1105, 227, 247, 1105, 1, 99999, 1005, 227, 99999, 1005,
0, 256, 1105, 1, 99999, 1106, 227, 99999, 1106, 0, 265, 1105, 1, 99999, 1006, 0, 99999, 1006, 227, 274,
1105, 1, 99999, 1105, 1, 280, 1105, 1, 99999, 1, 225, 225, 225, 1101, 294, 0, 0, 105, 1, 0, 1105, 1,
99999, 1106, 0, 300, 1105, 1, 99999, 1, 225, 225, 225, 1101, 314, 0, 0, 106, 0, 0, 1105, 1, 99999, 107,
677, 677, 224, 1002, 223, 2, 223, 1006, 224, 329, 101, 1, 223, 223, 108, 677, 677, 224, 1002, 223, 2,
223, 1005, 224, 344, 101, 1, 223, 223, 1007, 677, 677, 224, 1002, 223, 2, 223, 1006, 224, 359, 101, 1,
223, 223, 1107, 226, 677, 224, 1002, 223, 2, 223, 1006, 224, 374, 1001, 223, 1, 223, 8, 677, 226, 224,
1002, 223, 2, 223, 1006, 224, 389, 101, 1, 223, 223, 1108, 677, 677, 224, 1002, 223, 2, 223, 1005, 224,
404, 1001, 223, 1, 223, 7, 226, 226, 224, 1002, 223, 2, 223, 1006, 224, 419, 101, 1, 223, 223, 1107, 677,
677, 224, 1002, 223, 2, 223, 1005, 224, 434, 101, 1, 223, 223, 107, 226, 226, 224, 102, 2, 223, 223,
1005, 224, 449, 101, 1, 223, 223, 107, 677, 226, 224, 1002, 223, 2, 223, 1006, 224, 464, 1001, 223, 1,
223, 8, 226, 677, 224, 102, 2, 223, 223, 1006, 224, 479, 1001, 223, 1, 223, 108, 677, 226, 224, 102, 2,
223, 223, 1005, 224, 494, 1001, 223, 1, 223, 1108, 677, 226, 224, 1002, 223, 2, 223, 1005, 224, 509,
1001, 223, 1, 223, 1107, 677, 226, 224, 1002, 223, 2, 223, 1005, 224, 524, 101, 1, 223, 223, 1008, 226,
226, 224, 1002, 223, 2, 223, 1006, 224, 539, 101, 1, 223, 223, 1008, 226, 677, 224, 1002, 223, 2, 223,
1005, 224, 554, 1001, 223, 1, 223, 7, 226, 677, 224, 1002, 223, 2, 223, 1005, 224, 569, 101, 1, 223, 223,
1007, 677, 226, 224, 1002, 223, 2, 223, 1006, 224, 584, 1001, 223, 1, 223, 7, 677, 226, 224, 102, 2, 223,
223, 1006, 224, 599, 101, 1, 223, 223, 1007, 226, 226, 224, 102, 2, 223, 223, 1006, 224, 614, 101, 1,
223, 223, 1008, 677, 677, 224, 1002, 223, 2, 223, 1006, 224, 629, 101, 1, 223, 223, 108, 226, 226, 224,
102, 2, 223, 223, 1006, 224, 644, 101, 1, 223, 223, 1108, 226, 677, 224, 1002, 223, 2, 223, 1005, 224,
659, 101, 1, 223, 223, 8, 226, 226, 224, 1002, 223, 2, 223, 1005, 224, 674, 101, 1, 223, 223, 4, 223, 99,
226]
computer = IntcodeComputer(program, 1)
setup_logger('Computer 1', 'comp1.log')
data = deque()
data.append(5)
while not computer.halted:
computer.run(data)
if not computer.halted:
output = data.pop()
print(f'Diagnostic program output is {output}')
def run_day7_test1():
"""
Solve Advent of Code 2019 day 7 test 1.
Runs the first day 7 test program, and verifies the result.
:return: None
"""
answer: int = 0
test1 = [3, 15, 3, 16, 1002, 16, 10, 16, 1, 16, 15, 15, 4, 15, 99, 0, 0]
test1_phase = [4, 3, 2, 1, 0]
test1_answer = 43210
queue = deque()
computers = []
running_computers = 5
for machine in range(5):
setup_logger(f'Computer {machine}', f'comp{machine}.log')
computers.append(IntcodeComputer(test1, machine))
for n, phase in enumerate(test1_phase):
if n == 0:
queue.appendleft(0)
queue.appendleft(phase)
else:
queue.appendleft(phase)
if not computers[n].halted:
print(f'Running computer {n}')
computers[n].run(queue)
if computers[n].halted:
print(f'Computer {n} halted.')
running_computers -= 1
if not running_computers:
break
for comp_id in cycle(range(5)):
if not computers[comp_id].halted:
print(f'Running computer {comp_id}')
computers[comp_id].run(queue)
if computers[comp_id].halted:
print(f'Computer {comp_id} halted.')
running_computers -= 1
if not running_computers:
break
output = queue.pop()
answer = output if output > answer else answer
print(f'Test1 {"passed" if test1_answer == answer else "Failed"}, expected {test1_answer}, got {answer}.')
def run_day7_part1():
"""
Solve Advent of Code 2019 day 7 part 1.
Runs day 7 part 1's solution, and prints the result to the console.
:return: None
"""
program = [3, 8, 1001, 8, 10, 8, 105, 1, 0, 0, 21, 38, 55, 72, 93, 118, 199, 280, 361, 442, 99999, 3, 9, 1001, 9, 2,
9, 1002, 9, 5, 9, 101, 4, 9, 9, 4, 9, 99, 3, 9, 1002, 9, 3, 9, 1001, 9, 5, 9, 1002, 9, 4, 9, 4, 9, 99, 3,
9, 101, 4, 9, 9, 1002, 9, 3, 9, 1001, 9, 4, 9, 4, 9, 99, 3, 9, 1002, 9, 4, 9, 1001, 9, 4, 9, 102, 5, 9,
9, 1001, 9, 4, 9, 4, 9, 99, 3, 9, 101, 3, 9, 9, 1002, 9, 3, 9, 1001, 9, 3, 9, 102, 5, 9, 9, 101, 4, 9, 9,
4, 9, 99, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 1001, 9, 1, 9, 4, 9, 3, 9, 102, 2, 9, 9, 4, 9,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101, 2, 9, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 101, 1, 9,
9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 99]
phases: list = [0, 1, 2, 3, 4]
answer: int = 0
phase_permutations = permutations(phases)
communication_queue = deque()
computers = []
for machine in range(5):
setup_logger(f'Computer {machine}', f'comp{machine}.log')
computers.append(IntcodeComputer(program, machine))
for permutation in phase_permutations:
print(f"Permutation: {permutation}")
communication_queue.clear()
running_computers = 5
for n, phase in enumerate(permutation):
if n == 0:
communication_queue.appendleft(0) # oi, janky hack m8
communication_queue.appendleft(phase)
else:
communication_queue.appendleft(phase)
if not computers[n].halted:
computers[n].run(communication_queue)
if computers[n].halted:
running_computers -= 1
for comp_id in cycle(range(5)):
if not computers[comp_id].halted:
computers[comp_id].run(communication_queue)
if computers[comp_id].halted:
running_computers -= 1
if not running_computers:
break
output = communication_queue.pop()
answer = output if output > answer else answer
print(f'Answer is: {answer}')
def run_day7_part2():
"""
Solve Advent of Code 2019 day 7 part 2.
Runs day 7 part 2's solution, and prints the result to the console.
:return: None
"""
program = [3, 8, 1001, 8, 10, 8, 105, 1, 0, 0, 21, 38, 55, 72, 93, 118, 199, 280, 361, 442, 99999, 3, 9, 1001, 9, 2,
9, 1002, 9, 5, 9, 101, 4, 9, 9, 4, 9, 99, 3, 9, 1002, 9, 3, 9, 1001, 9, 5, 9, 1002, 9, 4, 9, 4, 9, 99, 3,
9, 101, 4, 9, 9, 1002, 9, 3, 9, 1001, 9, 4, 9, 4, 9, 99, 3, 9, 1002, 9, 4, 9, 1001, 9, 4, 9, 102, 5, 9,
9, 1001, 9, 4, 9, 4, 9, 99, 3, 9, 101, 3, 9, 9, 1002, 9, 3, 9, 1001, 9, 3, 9, 102, 5, 9, 9, 101, 4, 9, 9,
4, 9, 99, 3, 9, 101, 1, 9, 9, 4, 9, 3, 9, 1001, 9, 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 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 101, 1, 9,
9, 4, 9, 3, 9, 101, 2, 9, 9, 4, 9, 3, 9, 1002, 9, 2, 9, 4, 9, 3, 9, 101, 1, 9, 9, 4, 9, 99]
# tests:
# test1 = [3, 15, 3, 16, 1002, 16, 10, 16, 1, 16, 15, 15, 4, 15, 99, 0, 0]
# test2 = [3, 23, 3, 24, 1002, 24, 10, 24, 1002, 23, -1, 23, 101, 5, 23, 23, 1, 24, 23, 23, 4, 23, 99, 0, 0]
# test3 = [3, 31, 3, 32, 1002, 32, 10, 32, 1001, 31, -2, 31, 1007, 31, 0, 33, 1002, 33, 7, 33, 1, 33, 31, 31,
# 1, 32, 31, 31, 4, 31, 99, 0, 0, 0]
# program = test7 # Uncomment and modify this line to select one of the additional tests above.
phases: list = [5, 6, 7, 8, 9]
answer: int = 0
phase_permutations = permutations(phases)
communication_queue = deque()
for permutation in phase_permutations:
communication_queue.clear()
computers = []
running_computers = 5
for machine in range(5):
setup_logger(f'Computer {machine}', f'comp{machine}.log', level=logging.DEBUG)
computers.append(IntcodeComputer(program, machine))
for n, phase in enumerate(permutation):
if n == 0:
communication_queue.appendleft(0) # oi, janky hack m8
communication_queue.appendleft(phase)
else:
communication_queue.appendleft(phase)
if not computers[n].halted:
computers[n].run(communication_queue)
if computers[n].halted:
running_computers -= 1
for comp_id in cycle(range(5)):
if not computers[comp_id].halted:
computers[comp_id].run(communication_queue)
if computers[comp_id].halted:
running_computers -= 1
if not running_computers:
break
output = communication_queue.pop()
answer = output if output > answer else answer
print(f'Answer is: {answer}')
##############################################
# Main Function
##############################################
if __name__ == '__main__':
run_day7_part1()
pass