Advent of Code 2026 - Day 5, in Kotlin - Rhythmic Factory
Kotlin solutions to parts 1 and 2 of Advent of Code 2026, Day 5: 'Rhythmic Factory'
Day 5 of Advent of Code brings us not one, but two Breadth-First Searches! I’ve written my fair share of these over the years, and really admire how well they lend themselves to certain problems such as today’s.
If you’d rather just view the code, my GitHub Repository is here.
Puzzle Input
Our first order of business today is to parse the input. We’ll eventually turn the single String input into a List<(Int)->Int>, which is a list of functions that take an integer as an argument and emit an integer as its return type. Given the current display value on the fancy microwave, generate the next value.
class Day05(input: String) {
private val functions = parse(input)
private fun parse(input: String): List<(Int) -> Int> =
input.split(" ").map(::parseToken)
private fun parseToken(token: String): (Int) -> Int =
when {
token == "[bksp]" -> { it -> it / 10 }
token == "[oct]" -> { it -> it.toString(8).toInt() }
token.startsWith("[/") -> { it -> it / token[2].digitToInt() }
token.startsWith("[*") -> { it -> it * token[2].digitToInt() }
token.contains("->") -> { it -> it.toString().replace(token[1], token[4]).toInt() }
else -> { it -> (it * 10) + token[1].digitToInt() }
}
}
I broke parse and parseToken into two parts so I could skip having to define the types of the inline function definitions, which Kotlin was getting confused about when inside the map. Thankfully, Kotlin’s integer math works out the way the problem description wants it to behave, so no extra special handling required here.
⭐ Day 5, Part 1
The puzzle text can be found here.
To solve part one, we’ll create a Breadth-First Search (BFS). If you’ve done Advent of Code before, this algorithm should be familiar. For those of you seeing this for the first time, we have a queue where candidate states live, in some kind of order (in our case, these are naturally ordered by the number of button presses). We also have a seen set to record states we’ve been in. One of the things that make BFS work is that if we’ve seen a state already, we know we can abandon any future work that got us to that state, since we know it must have taken longer. While our queue has some work for us to do (we see the queue with a natural starting point), we add the result to seen and decide which new states should go on the queue.
// In Day05
fun solvePart1(): Int {
val queue = ArrayDeque<Pair<Int, Int>>().apply { add(0 to 0) }
val seen = mutableSetOf<Int>()
while (queue.isNotEmpty()) {
val (total, count) = queue.removeFirst()
if (total !in seen && count <= 3) {
seen += total
functions.forEach { fn ->
queue.add(fn.invoke(total) to count + 1)
}
}
}
return seen.sum()
}
In our case here, our carried state is a Pair<Int,Int> where the first integer is the total displayed on the fancy microwave, and the second is the count, or number of button presses it took to get there. If we’ve never seen the display value before, we want to add it to the seen set and then invoke each of the functions on that total, adding each result to the queue, with an increased count. We’ll skip that part if we generate a state that has more than 3 button presses.
Eventually, our BFS works through all three-button presses and stores all of the unique display values in seen. Since Set<Int> in Kotlin has a handy sum function, we can use that to get our answer.
Star earned! Onward!
⭐ Day 5, Part 2
The puzzle text can be found here.
Originally, I had solved parts 1 and 2 with the same BFS, but found it confusing. Ultimately, this version does not care how many button presses we do (part 1 needs at most 3). Similarly, part 1 does not care about the maximum value displayed (although I should note that I don’t think it’s possible to get to a five digit number given my rules and only 3 presses). So I wrote a new BFS, which works the same way as before with some minor changes.
// In Day05
fun solvePart2(): Int {
val queue = ArrayDeque<Int>().apply { add(0) }
val seen = mutableSetOf<Int>()
while (queue.isNotEmpty()) {
val total = queue.removeFirst()
if (total !in seen) {
seen += total
functions.forEach { fn ->
val next = fn.invoke(total)
if (next < 10_000) queue.add(next)
}
}
}
return seen.sum()
}
The two major changes are that we don’t count how many presses we have any longer, but we do keep track of the next value from the function invocations and check to see if we’ve crosses over the 10,000 mark. If we have, we can ignore that next value and not add it to the queue.
At the end of this, we have seen all possible states where the display is less than 5 digits. In my input, it took 12 presses max.
Star earned! See you tomorrow!
Further Reading
- Index of All Solutions - All posts and solutions for 2026, in Kotlin.
- My Github repo - Solutions and tests for each day.
- Solution - Full code for day 5
- Advent of Code - Come join in and do these challenges yourself!