Add Kotlin solution to day 8 part 1

This commit is contained in:
William Brawner 2023-12-08 22:44:20 -07:00
parent 8ac325848b
commit 399e90b1b7
Signed by: wbrawner
GPG key ID: 8FF12381C6C90D35

40
src/Day08.kt Normal file
View file

@ -0,0 +1,40 @@
fun main() {
fun part1(input: List<String>): Int {
val stepOrder = input.first()
val nodes = input.slice(2..input.lastIndex).toNodes()
var currentNode = "AAA"
var stepIndex = 0
var steps = 0
while (currentNode != "ZZZ") {
val step = stepOrder[stepIndex]
currentNode = nodes.require(currentNode).require(step)
steps++
if (++stepIndex == stepOrder.length) {
stepIndex = 0
}
}
return steps
}
fun part2(input: List<String>): Int {
throw RuntimeException("Day 8 part 2 was done in lua, see Day08.lua")
}
// test if implementation meets criteria from the description, like:
val testInput = readInput("Day08_test")
check(part2(testInput) == 6)
val input = readInput("Day08")
part1(input).println()
part2(input).println()
}
fun List<String>.toNodes(): Map<String, Map<Char, String>> = associate { line ->
val letterGroups = Regex("\\w+").findAll(line).map { it.value }.toList()
letterGroups.first() to mapOf(
'L' to letterGroups[1],
'R' to letterGroups[2],
)
}
fun <K, V> Map<K, V>.require(key: K): V = requireNotNull(get(key))