Kotlin
Part 1 was very straightforward, just a simple, cached recursive crawler.
In hindsight, part 2 works great with the same code and a small restriction. This took me quite a few off-by-one errors and Integer overflows to properly code though.
Solution
class Day11 : Puzzle {
val links = mutableMapOf<String, List<String>>()
val input1 = "you"
val input2 = "svr"
val output = "out"
val cache = mutableMapOf<Pair<String, Set<Pair<String, Boolean>>>, Long>()
override fun readFile() {
val input = readInputFromFile(2025, 11, false)
for (line in input.lines().filter { it.isNotBlank() }) {
val split = line.split(": ")
links[split[0]] = split[1].split(" ")
}
}
override fun solvePartOne(): String {
return travel(input1, setOf()).toString()
}
override fun solvePartTwo(): String {
val passed = listOf("dac", "fft")
.map { Pair(it, false) }
.toSet()
return travel(input2, passed).toString()
}
private fun travel(input: String, passed: Set<Pair<String, Boolean>>): Long {
if (cache.containsKey(Pair(input, passed))) return cache[Pair(input, passed)]!!
if (input == output) return if (passed.all { it.second }) 1L else 0L
val newPassed = if (input in passed.map { it.first }) {
passed.map { if (it.first == input) input to true else it }.toSet()
} else {
passed
}
val paths = links[input]!!
val sum = paths.sumOf { travel(it, newPassed) }
cache[Pair(input, newPassed)] = sum
return sum
}
}
full code on Codeberg