class Day06(input: List) { // Note that this is Orbiting -> Orbited, not Orbited -> Orbiting private val orbitPairs: Map = input.map { it.split(")") }.associate { it.last() to it.first() } fun solvePart1(): Int = orbitPairs.keys.sumBy { pathTo(it).size } fun solvePart2(): Int { val youToRoot = pathTo("YOU") val santaToRoot = pathTo("SAN") val intersection = youToRoot.intersect(santaToRoot).first() return youToRoot.indexOf(intersection) + santaToRoot.indexOf(intersection) -2 } private fun pathTo(orbit: String, path: MutableList = mutableListOf()): MutableList = orbitPairs[orbit]?.let { path.add(orbit) pathTo(it, path) } ?: path }