Add D6P2 in Kotlin
This commit is contained in:
parent
edcb443b4c
commit
9396c143d3
|
@ -1,8 +1,28 @@
|
|||
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()).first()
|
||||
println("Part 1: ${countOrbiters(input, rootNode, 0)}")
|
||||
val input = generateSequence(::readLine).map { it.split(")") }.toList()
|
||||
val parentToChildren = input.groupBy { it[0] }.mapValues { it.value.map { it[1] } }
|
||||
val childToParent = input.associateBy { it[1] }.mapValues { it.value[0] }
|
||||
val rootNode = (parentToChildren.keys - parentToChildren.values.flatten().toSet()).first()
|
||||
println("Part 1: ${countOrbiters(parentToChildren, rootNode, 0)}")
|
||||
val santaParents = getParents(childToParent, "SAN")
|
||||
val myParents = getParents(childToParent, "YOU")
|
||||
val commonParent = firstCommon(santaParents, myParents)
|
||||
println("Part 2: ${santaParents.indexOf(commonParent) + myParents.indexOf(commonParent)}")
|
||||
}
|
||||
|
||||
fun <T>countOrbiters(graph: Map<T, List<T>>, key: T, acc: Int): Int =
|
||||
fun <T> countOrbiters(graph: Map<T, List<T>>, key: T, acc: Int): Int =
|
||||
acc + (graph[key]?.map { countOrbiters(graph, it, acc+1) }?.sum() ?: 0)
|
||||
|
||||
fun <T> getParents(graph: Map<T, T>, key: T): List<T> =
|
||||
graph[key]?.let { curr -> listOf(curr) + getParents(graph, curr) } ?: emptyList()
|
||||
|
||||
fun <T> firstCommon(first: List<T>, second: List<T>): T {
|
||||
val seen = mutableSetOf<T>()
|
||||
for (i in 0..first.size) {
|
||||
if (first[i] in seen) return first[i]
|
||||
seen.add(first[i])
|
||||
if (second[i] in seen) return second[i]
|
||||
seen.add(second[i])
|
||||
}
|
||||
error("should be unreachable")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user