Day 03 - Lobby

greedysubsequenceparsing

Language: Rust

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


Part 1: Each line is a bank of batteries, each digit being a joltage rating. The goal is to pick two digits from each line, keeping their order, to form the largest possible two-digit number, then sum the results across all banks.

The idea is simple: for every digit, ask what’s the best left digit seen so far that I could pair with this one? I keep track of largest_seen and update the best candidate as I go:

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

Part 2: Now I need to pick 12 digits in order to form the largest possible number. The two-digit greedy trick doesn’t scale, so I switched to a stack-based approach.

I build the result left to right. When a new digit is larger than the last chosen one, it might be worth swapping it in, but only if there are still enough digits left in the line to fill the remaining spots. If the conditions check out, I pop and replace:

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

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

Once the stack has 12 digits, they fold into the final number:

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

This one took me a while. I hadn’t used stack-based greedy before, but it fits “largest subsequence” problems really well once it clicks.


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

No C writeup yet.