/
day07.py
64 lines (52 loc) · 1.95 KB
/
day07.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
import intcode
from itertools import permutations
def star1():
print("Star 1")
# Get all permutations of [0, 1, 2, 3, 4]
perm = permutations([0, 1, 2, 3, 4])
stages = []
for i in range(5):
stages.append(intcode.IntCodeComputer())
with open('puzzles/day7.txt', 'r') as f:
program = f.readline()
maxOutput = 0
for p in list(perm):
prevStageOutput = 0
for i in range(len(stages)):
stages[i].loadProgram(program)
stages[i].process(str(p[i]) + '\n' + str(prevStageOutput))
prevStageOutput = stages[i].lastOutput
finalStageOutput = stages[-1].lastOutput
if finalStageOutput > maxOutput:
maxOutput = finalStageOutput
print("Max Thruster Output:", maxOutput)
def star2():
print("Star 2")
# Get all permutations
perm = permutations([5, 6, 7, 8, 9])
stages = []
for i in range(5):
stages.append(intcode.IntCodeComputer())
with open('puzzles/day7.txt', 'r') as f:
program = f.readline()
maxOutput = 0
# Iterate over permutations of phase codes
for p in list(perm):
prevStageOutput = 0
for i in range(len(stages)):
# For each set of phase codes, load the program
# and add the phase code as the first input
stages[i].loadProgram(program)
stages[i].addInput(p[i])
while not stages[0].isTerminated:
for i in range(len(stages)):
#print("&&& PROCESSING {0} &&&".format(i))
stages[i].process(prevStageOutput, silent=True)
#print("Stage {0} Complete. Instruction Pointer: {1}. Last Output: {2}".format(i, stages[i].instrPtr, stages[i].lastOutput))
prevStageOutput = stages[i].lastOutput
finalStageOutput = prevStageOutput
if finalStageOutput > maxOutput:
maxOutput = finalStageOutput
print("Max Thruster Output:", maxOutput)
star1()
star2()