Advent of Code - Day 3: Lobby


2025-12-03.png

You descend a short staircase, enter the surprisingly vast lobby, and are quickly cleared by the security checkpoint. When you get to the main elevators, however, you discover that each one has a red light above it: they’re all offline.

“Sorry about that,” an Elf apologizes as she tinkers with a nearby control panel. “Some kind of electrical surge seems to have fried them. I’ll try to get them online soon.”

-- Day 3 - Advent of Code 2025

Solution in Java

Full source can be found: in GitHub

Each line of input represents a bank of batteries, where every digit is a battery with a joltage rating. Batteries must be selected in order, and the selected digits are concatenated to form the output joltage. The solution converts each line into a list of digits and then applies a greedy strategy to pick the highest possible digits while respecting the ordering constraint.

Part 1 - Selecting Two Batteries

In Part 1, exactly two batteries must be turned on per bank to form the largest possible two-digit number.

The solution first converts the line into a list of integers. To ensure a valid two-digit result, the search for the first digit excludes the final position. The highest possible digit is chosen as the first digit, and then the highest digit appearing after it is chosen as the second digit.

 1public void part1() {  
 2    int zeroOffset = '0';  
 3    long totalJoltages = 0;  
 4    for (String line : inputLoader.split(System.lineSeparator())) {  
 5        List<Integer> numbers = line.chars().map(c -> c - zeroOffset).boxed().toList();  
 6  
 7        int highestNumber = allButLast.stream().max(Integer::compareTo).orElseThrow();  
 8        int firstIndex = allButLast.indexOf(highestNumber);  
 9  
10        // Locate the second-highest number  
11        List<Integer> fromHighestToEnd = numbers.subList(firstIndex + 1, numbers.size());  
12        int secondHighestNumber = fromHighestToEnd.stream().max(Integer::compareTo).orElseThrow();  
13  
14        totalJoltages += Integer.parseInt("" + highestNumber + secondHighestNumber);  
15    }  
16  
17    System.out.println(totalJoltages);  
18}

This guarantees the maximum valid two-digit joltage for each bank.

Part 2 - Selecting Twelve Batteries

Part 2 extends the problem by requiring exactly twelve batteries per bank, producing a twelve-digit number.

Here, the joltage is built digit by digit. For each position, the algorithm selects the highest possible digit that still leaves enough remaining digits to complete a twelve-digit number. Once a digit is chosen, all earlier digits are discarded, preserving order while maximizing the result.

 1public void part2() {  
 2    int zeroOffset = '0';  
 3    long totalJoltages = 0;  
 4    for (String line : inputLoader.split(System.lineSeparator())) {  
 5        List<Integer> numbers = line.chars().map(c -> c - zeroOffset).boxed().toList();  
 6  
 7        int lowerBound = 0;  
 8        String foundJoltage = "";  
 9        for (int desiredDigit = 12; desiredDigit > 0; desiredDigit--) {  
10            List<Integer> fromDesiredToEnd = numbers.subList(lowerBound, numbers.size() - desiredDigit + 1);  
11  
12            // Find the highest in range  
13            int highestNumber = fromDesiredToEnd.stream().max(Integer::compareTo).orElseThrow();  
14            lowerBound += fromDesiredToEnd.indexOf(highestNumber) + 1;  
15  
16            foundJoltage += highestNumber;  
17        }  
18        totalJoltages += Long.parseLong(foundJoltage);  
19    }  
20  
21    System.out.println(totalJoltages);  
22}

The solution relies on simple but effective greedy selection. Part 1 focuses on choosing the best possible digit pair, while Part 2 generalizes the idea to longer sequences. By carefully limiting the search range and preserving digit order, the implementation remains both concise and efficient.


See also