Think Day 8 was pretty straightforward. Spoiler i...
# advent-of-code
a
Think Day 8 was pretty straightforward. Spoiler in thread.
Copy code
class Node(iterator: Iterator<Int>){
    val childCount = iterator.next()
    val metaDataCount = iterator.next()

    val childNodes = (0 until childCount).map { Node(iterator) }
    val metadata = (0 until  metaDataCount).map { iterator.next() }

    val totalPart1 : Int by lazy{ metadata.sum() + childNodes.map { it.totalPart1 }.sum() }
    val totalPart2 : Int by lazy{
        when (childCount){
            0 -> metadata.sum()
            else -> metadata.filter { it in (1 .. childNodes.size) }.map{ childNodes[it-1].totalPart2 }.sum()
        }
    }
}

fun solve(input:List<Int>): Pair<Int,Int>{
    val root = Node(input.listIterator())
    return Pair(root.totalPart1, root.totalPart2)
}
j
Nice! You can replace the
map
and
sum
with
sumBy
for further brevity
👍 1
g
The nullpointer you solved with the filter was an nice surprice, it's also a nice way to do it, I have a loop with
if(i == 0 || i > node.childCount) continue