Advent of Code 2025 - Day 6: Trash Compactor
Day 6 was a breath of fresh air after the complexity of Days 3-5. The problem involved parsing a math worksheet where problems are arranged vertically in columns. Both parts were straightforward grid manipulation.
Part 1: Vertical Math Problems
The worksheet has numbers stacked vertically with operators at the bottom. Each column is a separate problem, and we need to sum all the answers.
Example:
|
|
This represents four problems:
123 * 45 * 6 = 33210328 + 64 + 98 = 49051 * 387 * 215 = 424345564 + 23 + 314 = 401
Grand total: 4,277,556
The Solution
Grid problems are my favorite, and this one was particularly clean:
|
|
The regex \d*[^\s]|\*|\+ captures:
\d*[^\s]: Numbers (with any number of digits)|\*: Multiplication operator|\+: Addition operator
The key step: zip(*grid[:-1]) transposes the grid. If we have:
|
|
Transposing gives:
|
|
Now each row is a problem. Check the operator: multiply with math.prod(), add with sum().
First try!
Part 2: Right-to-Left Column Reading
Part 2 reveals the actual format: cephalopod math is read right-to-left, with each digit in its own column. Problems are separated by blank columns.
Same example, but reading columns right-to-left:
|
|
Now becomes:
- Rightmost:
4 + 431 + 623 = 1058(reading column digits top-to-bottom: 4, 431, 623) - Second:
175 * 581 * 32 = 3253600 - Third:
8 + 248 + 369 = 625 - Leftmost:
356 * 24 * 1 = 8544
Grand total: 3,263,827
The Whitespace Problem
Initially I struggled because my loadFile() helper strips trailing whitespace with .rstrip(). This removes the trailing spaces that separate problems! The fix: only strip newlines:
|
|
The Solution
|
|
Two Subtle Bugs
Both caught using the example input:
1. Multiplication starting at zero
If the first operator is *, we’d do 0 * X = 0. Fix: initialize tmp = 1 when multiplying from zero.
2. Missing the last problem
I only added tmp to score when hitting a blank column. The last problem doesn’t end with a blank column, so I needed to add score += tmp after the loop.
First try!
The Takeaway
After Day 3’s skip-budget algorithm, Day 4’s queue optimization, and Day 5’s range merging struggles, Day 6 was refreshingly straightforward. Grid transposition, regex parsing, and careful handling of edge cases.
The lesson: sometimes the hard part isn’t the algorithm - it’s the details. Preserving whitespace, handling the first multiplication, and remembering the last problem were all small things that could have caused wrong answers.
Twelve stars total. A nice palate cleanser before whatever complexity tomorrow brings.