Lost quite some time today in part 1
# advent-of-code
k
Lost quite some time today in part 1
How did you guys convert the output to a grid? I noticed lists don't have a
split()
so I decided to convert to an actual string which took quite some time to get right.
output.toList().joinToString(separator = "") { it.toChar().toString() }.split("\n").map{it.map{it}}
r
Copy code
output.map { it.toChar()}.joinToString("").split("\n").map { it.toList() }
👍 1
j
Same, just used
.lines()
instead of .
split("\n")
:
Copy code
.map { it.toChar() }.toList().joinToString("").lines()
k
I knew there was such a function, just forgot the name. I though it was
splitLines
or something
j
my Intcode works on
Channel
s for I/O, so I added a
Flow
for it and transform it like this:
Copy code
@Suppress("BlockingMethodInNonBlockingContext")
private fun Flow<Char>.fullLines(): Flow<String> = flow {
    val builder = StringBuilder()
    collect {
        when (it) {
            '\n' -> emit(builder.toString()).also { builder.clear() }
            else -> builder.append(it)
        }
    }
}
this approach has some flaws (e.g. just forgets last line without any
\n
at the end) but is good enough for day17 🙂
j
I also have a coroutine-based Intcode computer with Channels, for part 2 I turned my output Channel into a teletype by launching this:
Copy code
launch {
   while (!stdout.isClosedForReceive) {
      stdout.receive().also { print(if (it < 255) it.toChar() else "Part 2: $it") }
   }
}