Taddeus Kroes 6 gadi atpakaļ
vecāks
revīzija
8b87e985e1
2 mainītis faili ar 56 papildinājumiem un 0 dzēšanām
  1. 55 0
      2019/07_amplifiers.py
  2. 1 0
      2019/input/7

+ 55 - 0
2019/07_amplifiers.py

@@ -0,0 +1,55 @@
+#!/usr/bin/env python3
+import sys
+from itertools import permutations
+
+def run(p, inputs):
+    pc = 0
+    while p[pc] != 99:
+        modes, opcode = divmod(p[pc], 100)
+
+        def modeswitch(offset):
+            value = p[pc + offset]
+            mode = modes // (10 ** (offset - 1)) % 10
+            return value if mode else p[value]
+
+        if opcode in (1, 2):
+            a = modeswitch(1)
+            b = modeswitch(2)
+            out = p[pc + 3]
+            p[out] = a + b if opcode == 1 else a * b
+            pc += 4
+        elif opcode == 3:
+            address = p[pc + 1]
+            p[address] = inputs.pop()
+            pc += 2
+        elif opcode == 4:
+            inputs = yield modeswitch(1)
+            pc += 2
+        elif opcode == 5:
+            pc = modeswitch(2) if modeswitch(1) else pc + 3
+        elif opcode == 6:
+            pc = modeswitch(2) if not modeswitch(1) else pc + 3
+        elif opcode in (7, 8):
+            a = modeswitch(1)
+            b = modeswitch(2)
+            out = p[pc + 3]
+            p[out] = int(a < b if opcode == 7 else a == b)
+            pc += 4
+
+def amplify(p, phases):
+    amps = []
+    signal = 0
+    for phase in phases:
+        amp = run(list(p), [signal, phase])
+        amps.append(amp)
+        signal = next(amp)
+    try:
+        while True:
+            for amp in amps:
+                signal = amp.send([signal])
+    except StopIteration:
+        return signal
+
+program = list(map(int, sys.stdin.read().split(',')))
+print(max(amplify(program, phases) for phases in permutations(range(5))))
+print(max(amplify(program, phases) for phases in permutations(range(5, 10))))

+ 1 - 0
2019/input/7

@@ -0,0 +1 @@
+3,8,1001,8,10,8,105,1,0,0,21,34,55,68,85,106,187,268,349,430,99999,3,9,1001,9,5,9,1002,9,5,9,4,9,99,3,9,1002,9,2,9,1001,9,2,9,1002,9,5,9,1001,9,2,9,4,9,99,3,9,101,3,9,9,102,3,9,9,4,9,99,3,9,1002,9,5,9,101,3,9,9,102,5,9,9,4,9,99,3,9,1002,9,4,9,1001,9,2,9,102,3,9,9,101,3,9,9,4,9,99,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,99,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,99