summaryrefslogtreecommitdiffstats
path: root/19/py/d07.py
diff options
context:
space:
mode:
Diffstat (limited to '19/py/d07.py')
-rw-r--r--19/py/d07.py80
1 files changed, 80 insertions, 0 deletions
diff --git a/19/py/d07.py b/19/py/d07.py
new file mode 100644
index 0000000..7ab2fe6
--- /dev/null
+++ b/19/py/d07.py
@@ -0,0 +1,80 @@
+import intcode
+import itertools
+import queue
+
+def pt1(input):
+ program = [int(x) for x in input[0].split(",")]
+ highest_signal = 0
+ highest_sequence = None
+ for phase_seq in list(itertools.permutations(list(range(0,5)))):
+ q = queue.Queue(5)
+ for phase in phase_seq:
+ q.put(phase)
+ amps = [intcode.Computer(program) for _ in range(5)]
+ for amp in amps:
+ amp.input = q.get()
+ signal = 0
+ for amp in amps:
+ while True:
+ amp.step()
+ if amp.input is None:
+ amp.input = signal
+ if amp.output is not None:
+ signal = amp.output
+ amp.output = None
+ break
+ if signal > highest_signal:
+ highest_signal = signal
+ highest_sequence = phase_seq
+ return (highest_sequence, highest_signal)
+
+def pt2(input):
+ program = [int(x) for x in input[0].split(",")]
+ highest_signal = 0
+ highest_sequence = None
+ for phase_seq in list(itertools.permutations(list(range(5,10)))):
+ q = queue.Queue(5)
+ for phase in phase_seq:
+ q.put(phase)
+ amps = [intcode.Computer(program) for _ in range(5)]
+ for amp in amps:
+ amp.input = q.get()
+
+ signal = 0
+ current_amp = 0
+ while True:
+ amp = amps[current_amp]
+ amp.step()
+ if amp.input is None:
+ if amp.phase_read == False:
+ amp.phase_read = True
+ amp.input = signal
+ else:
+ pass
+ if amp.output is not None:
+ signal = amp.output
+ amp.output = None
+ current_amp = (current_amp + 1) % 5
+ if amps[current_amp].phase_read == True:
+ amps[current_amp].input = signal
+ continue
+ if amp.memory[amp.pointer] == 99:
+ if current_amp == 4:
+ break
+ current_amp = (current_amp + 1) % 5
+ amps[current_amp].input = signal
+ continue
+ if signal > highest_signal:
+ highest_signal = signal
+ highest_sequence = phase_seq
+ return (highest_sequence, highest_signal)
+
+if __name__ == "__main__":
+ import cProfile
+
+ input = open("../input/07", "r").readlines()
+ cProfile.run("pt1(input)")
+ cProfile.run("pt2(input)")
+ print(pt1(input))
+ print(pt2(input))
+