Managed to implement my amplifiers for day 7 as vi...
# advent-of-code
j
Managed to implement my amplifiers for day 7 as virtual machines, running as coroutines. The input to each machine is a
ReceiveChannel<Int>
, its output a
SendChannel<Int>
s, and the channels are wired such that machine A's output channel is the input for machine B. Machine E's output is wired to both machine A's input as well as a separate output channel using a
BroadcastChannel<Int>
. Then it's just a matter of sending the input phases and the initial
0
to A, and off we go - no manual state management needed, and the data just flows between the coroutines. I'm very pleased I got this working, required a lot of tinkering and refactoring. My coroutine computer is <https://github.com/jorispz/aoc-2019/blob/master/src/commonMain/kotlin/day07/CoComputer.kt>, the solution iterating over the phases and setting up the channels is in <https://github.com/jorispz/aoc-2019/blob/master/src/commonMain/kotlin/day07/p07.kt>
👍 2
m
I’m actually currently working on the exact same thing right now 🙂 Right now I’m just getting deadlocks though.
j
Hang in there, watching these machines run concurrently is a very satisfying experience :-)
😎 1
k
I love the coroutine solution, very elegant! Last year I did something similar but it was a struggle because I still haven't learned coroutines properly.
👍 1