summary refs log tree commit diff stats
path: root/day18.py
diff options
context:
space:
mode:
Diffstat (limited to 'day18.py')
-rw-r--r--day18.py66
1 files changed, 66 insertions, 0 deletions
diff --git a/day18.py b/day18.py
new file mode 100644
index 0000000..15a9545
--- /dev/null
+++ b/day18.py
@@ -0,0 +1,66 @@
+import queue
+import collections
+import multiprocessing.pool
+
+with open("day18.txt") as program:
+    PROGRAM = [line.strip() for line in program]
+
+
+def run(ident, inqueue, outqueue):
+    regs = collections.defaultdict(int)
+    regs['p'] = ident
+
+    def val(v):
+        try:
+            return int(v)
+        except ValueError:
+            return regs[v]
+
+    pc = 0
+    count = 0
+    played = None
+
+    while 0 <= pc < len(PROGRAM):
+        cmd = PROGRAM[pc].split()
+        if cmd[0] == 'snd':
+            played = val(cmd[1])
+            if outqueue:
+                outqueue.put(val(cmd[1]))
+            count += 1
+        elif cmd[0] == 'set':
+            regs[cmd[1]] = val(cmd[2])
+        elif cmd[0] == 'add':
+            regs[cmd[1]] += val(cmd[2])
+        elif cmd[0] == 'mul':
+            regs[cmd[1]] *= val(cmd[2])
+        elif cmd[0] == 'mod':
+            regs[cmd[1]] %= val(cmd[2])
+        elif cmd[0] == 'rcv':
+            if inqueue:
+                try:
+                    regs[cmd[1]] = inqueue.get(timeout=5)
+                except queue.Empty:
+                    return count
+            elif regs[cmd[1]] != 0:
+                return played
+        elif cmd[0] == 'jgz':
+            if val(cmd[1]) > 0:
+                pc += val(cmd[2])
+                continue
+        pc += 1
+
+    return count
+
+
+print('PART 1:', run(0, None, None))
+
+pool = multiprocessing.pool.ThreadPool(processes=2)
+
+q1 = multiprocessing.Queue()
+q2 = multiprocessing.Queue()
+
+res1 = pool.apply_async(run, (0, q1, q2))
+res2 = pool.apply_async(run, (1, q2, q1))
+
+res1.get()
+print('PART 2:', res2.get())
\ No newline at end of file