summaryrefslogtreecommitdiffstats
path: root/solutions/py/d07.py
diff options
context:
space:
mode:
authorGustav Sörnäs <gusso230@student.liu.se>2019-12-08 11:06:56 +0100
committerGustav Sörnäs <gusso230@student.liu.se>2019-12-08 11:06:56 +0100
commit29e33f8f6a31565f5a2671b1c459ff1b829630f7 (patch)
treec051274a9e9a895049a077a6bdce9056dfb9afb1 /solutions/py/d07.py
parentd16fc72a33fced8c8623bea6cf9cdd9cf8999024 (diff)
downloadaoc-29e33f8f6a31565f5a2671b1c459ff1b829630f7.tar.gz
Refactor and create main.py
Diffstat (limited to 'solutions/py/d07.py')
-rw-r--r--solutions/py/d07.py70
1 files changed, 70 insertions, 0 deletions
diff --git a/solutions/py/d07.py b/solutions/py/d07.py
new file mode 100644
index 0000000..e12d7c9
--- /dev/null
+++ b/solutions/py/d07.py
@@ -0,0 +1,70 @@
+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)