-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_07b.py
155 lines (133 loc) · 4.98 KB
/
day_07b.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
#!/usr/bin/python3
import sys
import itertools
class AmplifierConfigurationProgram:
def __init__(self, program_str):
self.program_str = program_str
self.program = list(map(int, program_str.split(",")))
self.complete = False
self.inst_ptr = 0
def reset(self):
self.program = list(map(int, program_str.split(",")))
self.complete = False
self.inst_ptr = 0
def getNOps(self, n, modes):
ops = list()
for i in range(0, n):
rem = modes % 10
modes = int(modes / 10)
if rem == 0:
ops.append(self.program[self.program[self.inst_ptr + i + 1]])
else:
ops.append(self.program[self.inst_ptr + i + 1])
return ops
def compute(self, input):
output = None
while self.program[self.inst_ptr] != 99:
# print(inst_ptr)
instr = self.program[self.inst_ptr]
opcode = instr % 100
modes = int(instr / 100)
if opcode == 1:
ops = self.getNOps(2, modes)
self.program[self.program[self.inst_ptr + 3]] = ops[0] + ops[1]
self.inst_ptr += 4
elif opcode == 2:
ops = self.getNOps(2, modes)
self.program[self.program[self.inst_ptr + 3]] = ops[0] * ops[1]
self.inst_ptr += 4
elif opcode == 3:
self.program[self.program[self.inst_ptr + 1]] = input.pop(0)
self.inst_ptr += 2
elif opcode == 4:
output = self.program[self.program[self.inst_ptr + 1]]
self.inst_ptr += 2
return output
elif opcode == 5:
ops = self.getNOps(2, modes)
if ops[0] != 0:
self.inst_ptr = ops[1]
else:
self.inst_ptr += 3
elif opcode == 6:
ops = self.getNOps(2, modes)
if ops[0] == 0:
self.inst_ptr = ops[1]
else:
self.inst_ptr += 3
elif opcode == 7:
ops = self.getNOps(2, modes)
if ops[0] < ops[1]:
self.program[self.program[self.inst_ptr + 3]] = 1
else:
self.program[self.program[self.inst_ptr + 3]] = 0
self.inst_ptr += 4
elif opcode == 8:
ops = self.getNOps(2, modes)
if ops[0] == ops[1]:
self.program[self.program[self.inst_ptr + 3]] = 1
else:
self.program[self.program[self.inst_ptr + 3]] = 0
self.inst_ptr += 4
self.complete = True
return output
class IntcodeComputer:
def __init__(self):
self.AmplifierConfigurationPrograms = list()
def addAmplifierConfigurationProgram(self, program_str):
self.AmplifierConfigurationPrograms.append(
AmplifierConfigurationProgram(program_str)
)
def runAmplifierConfigurationProgram(self, index, input):
return self.AmplifierConfigurationPrograms[index].compute(input)
def resetAmplifierConfigurationProgram(self, ndex):
self.AmplifierConfigurationPrograms.reset()
def reset(self):
self.AmplifierConfigurationPrograms.clear()
def TestAmplifierConfigurationAndReset(computer, program_str, phase_setting):
output = 0
for i in range(0, len(phase_setting)):
computer.addAmplifierConfigurationProgram(program_str)
output = computer.runAmplifierConfigurationProgram(
i, [phase_setting[i], output]
)
computer.reset()
return output
def TestAmplifierConfigurationWithFeedbackAndReset(
computer, program_str, phase_setting
):
output = 0
last_valid = None
for i in range(0, len(phase_setting)):
computer.addAmplifierConfigurationProgram(program_str)
for i in range(0, len(phase_setting)):
output = computer.runAmplifierConfigurationProgram(
i, [phase_setting[i], output]
)
while not computer.AmplifierConfigurationPrograms[-1].complete:
for i in range(0, len(phase_setting)):
output = computer.runAmplifierConfigurationProgram(i, [output])
if output is not None:
last_valid = output
computer.reset()
return last_valid
def main():
# Get program and input
file_name = "../input/day_07_input"
if len(sys.argv) > 1 and not sys.argv[1].isnumeric():
file_name = sys.argv[1]
file = open(file_name)
program_str = file.readline().strip("\n")
# Solve
computer = IntcodeComputer()
max_output = 0
for phase_setting in itertools.permutations([5, 6, 7, 8, 9]):
output = TestAmplifierConfigurationWithFeedbackAndReset(
computer, program_str, phase_setting
)
if output > max_output:
max_output = output
print(max_output)
return max_output
if __name__ == "__main__":
main()