Shorten D6P1 in Kotlin
This commit is contained in:
parent
68f6629aa3
commit
edcb443b4c
|
@ -1,13 +1,8 @@
|
|||
fun main() {
|
||||
val input = generateSequence(::readLine)
|
||||
.map { it.split(")") }
|
||||
.groupBy { it[0] }
|
||||
.mapValues{ it.value.map { it[1] } }
|
||||
val rootNode = input.keys - input.values.flatten().toSet()
|
||||
|
||||
println("Part 1: ${countOrbiters(input, rootNode.first(), 0)}")
|
||||
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)}")
|
||||
}
|
||||
|
||||
fun <T>countOrbiters(graph: Map<T, List<T>>, key: T, acc: Int): Int {
|
||||
return acc + (graph[key]?.map { countOrbiters(graph, it, acc+1) }?.sum() ?: 0)
|
||||
}
|
||||
fun <T>countOrbiters(graph: Map<T, List<T>>, key: T, acc: Int): Int =
|
||||
acc + (graph[key]?.map { countOrbiters(graph, it, acc+1) }?.sum() ?: 0)
|
||||
|
|
Loading…
Reference in New Issue
Block a user