Contents

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:

1
2
3
4
123 328  51 64 
 45 64  387 23 
  6 98  215 314
*   +   *   +  

This represents four problems:

  • 123 * 45 * 6 = 33210
  • 328 + 64 + 98 = 490
  • 51 * 387 * 215 = 4243455
  • 64 + 23 + 314 = 401

Grand total: 4,277,556

The Solution

Grid problems are my favorite, and this one was particularly clean:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def part1():
    score = 0
    grid = []
    lines = loadFile(input_f)
    
    pattern = re.compile(r'\d*[^\s]|\*|\+')
    
    for l in lines:
        p = pattern.findall(l)
        try:  # Convert to integers except the last row (operators)
            grid.append(list2int(p))
        except:
            grid.append(p)
    
    # Transpose: each row becomes a column (problem)
    numbers = list(map(list, zip(*grid[:-1])))
    ops = grid[-1]
    
    for x in range(len(ops)):
        if ops[x] == '*':
            score += math.prod(numbers[x])
        else: 
            score += sum(numbers[x])
    
    return score

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:

1
2
3
[[123, 328, 51, 64],
 [45, 64, 387, 23],
 [6, 98, 215, 314]]

Transposing gives:

1
2
3
4
[[123, 45, 6],
 [328, 64, 98],
 [51, 387, 215],
 [64, 23, 314]]

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:

1
2
3
4
123 328  51 64 
 45 64  387 23 
  6 98  215 314
*   +   *   +  

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:

1
2
3
4
lines = []
with open(input_f) as file:
    for line in file:
        lines.append(line.rstrip('\n'))  # Keep trailing spaces

The Solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def part2(): 
    score = 0
    grid = []
    lines = []
    
    with open(input_f) as file:
        for line in file:
            lines.append(line.rstrip('\n'))
    
    for l in lines:
        p = list(l)  # Keep every character including spaces
        grid.append(p)
    
    # Transpose and remove spaces from operators
    numbers = list(map(list, zip(*grid[:-1])))
    ops = [x.strip(' ') for x in grid[-1] if x != ' ']
    
    count = 0  # Which operator we're on
    tmp = 0    # Running total for current problem
    
    for x in range(len(numbers)):
        # Blank column means problem separator
        if all(n == ' ' for n in numbers[x]):
            score += tmp
            tmp = 0
            count += 1
            continue
        
        op = ops[count]
        
        if op == '+':
            tmp += int(''.join(numbers[x]))
        else:
            if tmp == 0:
                tmp = 1  # Fix: 0 * anything = 0
            tmp *= int(''.join(numbers[x]))
    
    score += tmp  # Don't forget the last problem!
    
    return score

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.