Skip to Content

Advent of Code 2025 - Day 3, in Kotlin - Lobby

Kotlin solutions to parts 1 and 2 of Advent of Code 2025, Day 3: 'Lobby'

Posted on

When doing fix-it work at home, I’m usually pretty reluctant to get involved with electricity, being that it can kill you and whatnot. Nevertheless, let’s go play with some batteries today and do mad science stuff with joltages.

If you’d rather just view the code, my GitHub Repository is here.

Puzzle Input

Our algorithm today will depend on knowing not only the values of each of the batteries in each bank, but their index as well. To make calculating that easier, we’ll store this data as an IndexedValue. This is provided by the Kotlin Standard Library, by calling withIndex() on an Iterable. We’ll store each bank (List<IndexedValue<Int>>) in a List<> called banks.

class Day03(input: List<String>) {

    private val banks: List<List<IndexedValue<Int>>> = input.map { parseInput(it) }

    private fun parseInput(input: String): List<IndexedValue<Int>> =
        input.map { it.digitToInt() }.withIndex().toList()
}

⭐ Day 3, Part 1

The puzzle text can be found here.

Before we solve the puzzle, let’s think about each bank of batteries. We’ll pick a simple case: the bank is 3 batteries wide, and we only need 2 of them. Suppose it is 123. The answer is going to be 23. We can’t start with 3 because there is nothing to the right of it to pick, so we’ll “reserve” it, leaving either 1 or 2 to pick from for the first number. Since 2 is the greatest, we’ll pick that, and then our only remaining candidate, 3.

Now let’s expand our example. We have 5 batteries and want 3 of them. Suppose the bank is 54321. For the first battery, we’ll examine 5, 4, and 3, leaving 2 and 1 as “reserved” for the possible last two batteries. Taking 5, we now examine 4, 3, and 2, because 5 was taken and 1 is reserved. Last, we examine 3, 2, and 1, taking 3 for an answer of 543.

We’ll replicate this logic in our joltage function which takes a bank and how many batteries we want to consider.

// In Day03

private fun joltage(bank: List<IndexedValue<Int>>, batteries: Int): Long =
    (1 .. batteries).fold(0L to 0) { (total, leftIndex), offset ->
        bank.subList(leftIndex, bank.size - batteries + offset)
            .maxBy { it.value }
            .let { (total * 10) + it.value to it.index + 1 }
    }.first

First, we set up a range from 1 to the number of batteries, and fold over it. To seed the fold, we’ll use a Pair<Long, Int> where the Long is the running total (initially 0), and the Int is the leftmost index we can consider (initially 0). The leftmost index will increase as we find a new max value.

The body of the fold now has three variables, the total and leftIndex destructured from the Pair, and an offset from the range, representing each battery number. For each invocation, we look at a subList of the bank because that doesn’t create a new List, just a view on our original bank. We examine all elements between the leftIndex, and as far to the end of the list as we can go, taking into account “reservations” for future invocations.

We take the maxBy of the greatest value, and then calculate a new Pair<Long,Int> by multiplying the running total by 10 and adding the newly found max value, and then moving the leftIndex one over from the index of the max value we just found.

Because our fold returns the Pair<Long,Int>, and we only care about the Long value, we take the first element of the Pair and return it, for our joltage.

To solve part 1, we get the sumOf the total output from the joltage function when called for each bank. We pass in 2 to indicate how many batteries to look for.

// In Day03

fun solvePart1(): Long =
    banks.sumOf { joltage(it, 2) }

Star earned! Onward!

⭐ Day 3, Part 2

The puzzle text can be found here.

It turns out our joltage function works just fine when we need 12 batteries instead of 2, so we can reuse our code as-is.

// In Day03

fun solvePart1(): Long =
    banks.sumOf { joltage(it, 12) }

Star earned! See you tomorrow!

Further Reading

  1. Index of All Solutions - All posts and solutions for 2025, in Kotlin.
  2. My Github repo - Solutions and tests for each day.
  3. Solution - Full code for day 3
  4. Advent of Code - Come join in and do these challenges yourself!