Ok so I did the game, <https://github.com/jakubgwo...
# advent-of-code
j
Ok so I did the game, https://github.com/jakubgwozdz/AdventOfCode/blob/master/src/advent2019/day13/Day13.kt and it works, but I still have this feeling that I don't really understand channels in coroutines. main problem is that I wanted to create the lazy producer (ReceiveChannel) that is called a) only when it's
.receive()
is explicitly called and b) only after another channel (output from the Intcode) has been completely processed. I failed to do it, so instead I created a wrapper that does something like
Copy code
override suspend fun receive(): BigInteger {
    while (!output.isEmpty) {
        val x = output.receive()
        // ... process x
    }
    return something
}
But I think I'm missing some point here and it could be more elegant
j
I just pushed my Channel/Coroutine solution, maybe that can be of some help? https://github.com/jorispz/aoc-2019/blob/master/src/commonMain/kotlin/day13/p13.kt
k
Oh, you figured out you only need to input if a ball pos is given
j
Yeah, that was a guess, there's nothing in the text that says how often the joystick state is queried. For that reason I initially used a conflated channel that caches the latest value send, but it turned out to be unnecessary so I removed it again. Once per frame is enough
j
Yeah at some point I had this solution too, but I wanted to perform calculation for joystick only when it is requested
j
I was thinking about using
Flow
instead of
Channel
for exactly this reason, as they are essentially suspending sequences and should be able to lazily calculate the input at the time it was requested. But I struggled to come up with a working design and so went back to Channels. Would be an interesting thing to refactor to in the future
👍 1