Advent of Code 2025 Day 3: Lobby
from advent import parse, digits, Generator
input: tuple[tuple[int, ...]] = parse(3, digits)
def bank_digits(batteries: tuple[int, ...], num_batteries: int) -> Generator[int, None, None]:
for n in range(num_batteries, 0, -1):
yield (digit := max(batteries[: len(batteries) - (n - 1)]))
batteries = batteries[batteries.index(digit) + 1 :]
def solve(num_batteries: int) -> int:
return sum(
int(''.join([str(digit) for digit in bank_digits(batteries, num_batteries)]))
for batteries in input
)
assert solve(2) == 17087
assert solve(12) == 169019504359949
Input Parsing
----------------------------------------------------------------------------------------------------
app/day03.txt ➜ 20200 chars, 200 lines; first 3 lines:
----------------------------------------------------------------------------------------------------
4346343235149456543445233353534244533333333343433259333326337334334333438332533343452433223352443324
2323233732423333335633333322134234324554233323746324333322454233432477323332532436412434167322334333
4438535634336316544333233334654336327872333345433342236331325333833273333243433524422235474324281254
----------------------------------------------------------------------------------------------------
parse(3) ➜ 200 entries:
----------------------------------------------------------------------------------------------------
((4, 3, 4, 6, 3, 4, 3, 2, 3, 5, 1, 4, 9, 4, 5, 6, 5, 4, 3, 4, 4, 5, 2, ... 5, 4, 3, 1, 3, 4, 7, 2))
----------------------------------------------------------------------------------------------------
parse(3, digits) provides a tuple of banks, with each bank being a tuple of ints
Solution
Again, both parts are very similar today, and can be handled with a parameter indicating the
number of batteries to turn on. So, our solve(n) function:
- Iterates over each bank with
for batteries in input - For each bank:
- Generate battery numbers with
bank_digits() - Join the digits with
join() - Compute the joltage with
int()
- Generate battery numbers with
- Sums all of the bank joltages
I decided to use a Python generator function to produce the sequence of batteries. We need to produce N batteries, so the following process is repeated for
each battery:
- Find the max battery in the front of the list (ignoring enough of the tail of the list to select subsequent batteries), and
yieldits value. - Remove the begining of the bank list, up to and including the first occurrence of the yielded battery.
