diff options
author | Brian Chu <brianmchu42@gmail.com> | 2022-02-21 00:11:06 -0800 |
---|---|---|
committer | Brian Chu <brianmchu42@gmail.com> | 2022-02-21 00:11:06 -0800 |
commit | 0a4fe70d367f0bf1a78602af600052f58a377c34 (patch) | |
tree | 69c2bcd2b54b48968fd52728aaf0e21ca154784c /day18.py | |
parent | 1e2642d8793e6a4fb6cba16cd651d5fdca3e4581 (diff) | |
download | AdventOfCode2017-0a4fe70d367f0bf1a78602af600052f58a377c34.tar.gz |
solutions to day 25 main
Diffstat (limited to 'day18.py')
-rw-r--r-- | day18.py | 66 |
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 |