advent-of-code/2019/06/day06.kt

9 lines
416 B
Kotlin
Raw Normal View History

2019-12-06 09:56:14 +01:00
fun main() {
2019-12-06 10:16:50 +01:00
val input = generateSequence(::readLine).map { it.split(")") }.groupBy { it[0] }.mapValues{ it.value.map { it[1] } }
val rootNode = (input.keys - input.values.flatten().toSet()).first()
println("Part 1: ${countOrbiters(input, rootNode, 0)}")
2019-12-06 09:56:14 +01:00
}
2019-12-06 10:16:50 +01:00
fun <T>countOrbiters(graph: Map<T, List<T>>, key: T, acc: Int): Int =
acc + (graph[key]?.map { countOrbiters(graph, it, acc+1) }?.sum() ?: 0)