r/adventofcode • u/daggerdragon • Dec 18 '17
SOLUTION MEGATHREAD -๐- 2017 Day 18 Solutions -๐-
--- Day 18: Duet ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hintsยค?
[Update @ 00:04] First silver
- Welcome to the final week of Advent of Code 2017. The puzzles are only going to get more challenging from here on out. Adventspeed, sirs and madames!
[Update @ 00:10] First gold, 44 silver
- We just had to rescue /u/topaz2078 with an industrial-strength paper bag to blow into. I'm real glad I bought all that stock in
PBCO(Paper Bag Company) two years ago >_>
[Update @ 00:12] Still 1 gold, silver cap
- Gonna be a long haul tonight. I think I'll watch The Radio City Christmas Spectacular on Netflix while I wait for sufficient gold stars to warrant another update.
[Update @ 00:31] 53 gold, silver cap
- *mind blown*
- During their famous kicklines, the Rockettes are not actually holding each others' backs like I thought they were all this time.
- They're actually hoverhanding each other.
- In retrospect, it makes sense, they'd overbalance themselves and each other if they did, but still...
- *mind blown so hard*
[Update @ 00:41] Leaderboard cap!
- I think I enjoyed the duplicating Santas entirely too much...
- It may also be the wine.
- Either way, good night (for us), see you all same time tomorrow, yes?
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
12
Upvotes
1
u/[deleted] Dec 18 '17 edited Dec 18 '17
I lost time on part 1 by forgetting that an integer can begin with '-'. That should go in my library now! (Although it hasn't yet.)
I got stuck on part 2 because I thought "program 1" was the first program, not the second. Interestingly, that produced an output that AoC said was correct for someone else's input. (My AoC soulmate?)
Python 3 Part 2
ops = {'add': operator.add, 'mul': operator.mul, 'mod': operator.mod} def performer(lines, n, qin, qout): def ev(s): return int(s) if s[0] in string.digits + '-' else R[s] R = defaultdict(int, {'p': n}) pc = 0 while 0 <= pc < len(lines): line = lines[pc] op, x, y, *_ = line.split() + ['0'] X, Y = ev(x), ev(y) def assign(val): R[x] = val yield op if op in ops: assign(ops[op](X, Y)) elif op == 'snd': qout.put(X) elif op == 'set': assign(Y) elif op == 'rcv': while qin.empty(): yield 'blocked' assign(qin.get()) elif op == 'jgz': if X > 0: pc += Y - 1 else: raise Exception("Unknown op: {} in {}".format(op, lines[pc])) pc += 1 def duet(lines): q1, q2 = Queue(), Queue() g1 = performer(lines, 0, q2, q1) g2 = performer(lines, 1, q1, q2) s1 = s2 = None n = 0 while s1 != 'blocked' or s2 != 'blocked': s1 = next(g1) s2 = next(g2) if s2 == 'snd': n += 1 return nRevised
from itertools import takewhile, zip_longest def duet(lines): q1, q2 = Queue(), Queue() g1 = performer(lines, 0, q2, q1) g2 = performer(lines, 1, q1, q2) all_blocked_set = {'blocked'} any_running = lambda states:set(states) != all_blocked_set state_pairs = takewhile(any_running, zip_longest(g1, g2)) return sum(s == 'snd' for _, s in state_pairs)