Advent of Code 2025 — Day 03

Language: Rust

Problem https://adventofcode.com/2025/day/3


Day 3 is about picking digits from each line to form the largest possible number, without changing their order. Each line represents a bank of batteries, and the digits are their joltages.


Part 1: For part 1, only two digits are needed. The idea is pretty simple, for every digit, ask what is the best digit I’ve seen so far that I could pair with this one?

I keep track of:

Each new digit forms a candidate with largest_seen:

let joltage = largest_seen * 10 + digit;
max_joltage = max_joltage.max(joltage);
largest_seen = largest_seen.max(digit);

Part 2: Part 2 generalizes the problem: pick 12 digits in order to form the largest possible number.

This is solved using a greedy stack-based approach. I build the result left to right, keeping a stack of chosen digits:

while let Some(&last) = stack.last() {
    if last < d && stack.len() + (n - i - 1) >= k {
        stack.pop();
    } else {
        break;
    }w
}

if stack.len() < k {
    stack.push(d);
}

Once the stack contains 12 digits, they’re folded into the final number:

let number = stack.iter().fold(0, |acc, &d| acc * 10 + d as u128);

Closing thoughts This one took me some time to find and properly understand, but learning a bit about stack-based greedy algorithms paid off here. They fit this kind of “largest subsequence” problem perfectly, and it was genuinely fun to work through the reasoning and implementation. Hopefully there will be more problems like this later on.


Solution: https://github.com/Elyrial/AdventOfCode/blob/main/src/solutions/year2025/day03.rs

Approach (C)

Explain it.