Add day 6 part 1 in Kotlin

This commit is contained in:
kageru 2019-12-06 09:56:14 +01:00
parent 947f4d68df
commit 68f6629aa3
2 changed files with 1070 additions and 0 deletions

13
2019/06/day06.kt Normal file
View File

@ -0,0 +1,13 @@
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)}")
}
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)
}

1057
2019/06/input Normal file

File diff suppressed because it is too large Load Diff