This was a nice and simple solution as long as you remembered how to pivot a list of lists. Kotlin's builtin minBy and count functions also simplify things a lot.
minBy
count
import arrow.core.firstOrNone import arrow.core.getOrElse import java.nio.file.Files import java.nio.file.Paths object Day08 { private const val FILENAME = "src/main/resources/day08.txt" val fileData: String = Files.readAllLines(Paths.get(FILENAME)).first() private fun List<String>.countAllEqualTo(match: Char): Int { return sumBy { row -> row.count { pixel -> pixel == match } } } fun part1(input: String): Int { val layerWithLeast0s = input.chunked(25).chunked(6).minBy { layer -> layer.countAllEqualTo('0') }.orEmpty() val num1s = layerWithLeast0s.countAllEqualTo('1') val num2s = layerWithLeast0s.countAllEqualTo('2') return num1s * num2s } private fun List<String>.findPixelColors() = (this[0].indices).map { i -> map { it[i] } } .map(this@Day08::findPixelColor) private fun findPixelColor(it: List<Char>) = it.dropWhile { c -> c == '2' } .firstOrNone() .getOrElse { '2' } private fun List<List<Char>>.renderImage() = joinToString("\n") { row -> row.joinToString("") { pixel -> when (pixel) { '0' -> '.' '1' -> '#' '2' -> ' ' else -> '?' }.toString() } } fun part2(input: String) = input.chunked(25 * 6) .findPixelColors() .chunked(25) .renderImage() } fun main() { println(Day08.part1(Day08.fileData)) println(Day08.part2(Day08.fileData)) }
Here's a version of part1 that does everything in two passes of the list instead of four.
fun part1(input: String) = input.chunked(25 * 6) .asSequence() .map { layer -> layer.groupBy { it } .mapValues { (_, v) -> v.size } }.minBy { it.getOrDefault('0', 0) }?.let { it.getOrDefault('1', 0) * it.getOrDefault('2', 0) } ?: 0
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
This was a nice and simple solution as long as you remembered how to pivot a list of lists. Kotlin's builtin
minBy
andcount
functions also simplify things a lot.Here's a version of part1 that does everything in two passes of the list instead of four.