Advent of Code 2025 - Day 6

Published: (December 6, 2025 at 11:54 AM EST)
3 min read
Source: Dev.to

Source: Dev.to

Part one

The first part gives us a few rows of numbers and a last line with operations that are either addition (+) or multiplication (*). Our job is to apply these operations to those numbers, not across each row, but down each column.

Example input

123 328  51 64 
 45 64  387 23 
  6 98  215 314
*   +   *   +

The result of the first column is 123 * 45 * 6, the second column is 328 + 64 + 98, and so on.

We can solve this programmatically by splitting each row into numbers (or operations), then iterating over all rows with numbers and either adding or multiplying all numbers at the current position, from left to right. In the code below, the first line has already been added into the columns variable, giving us a starting value for each column. That’s why the loop starts at index 1 (problems[1:]).

for line in problems[1:]:
    for i in range(len(columns)):
        if operations[i] == '+':
            columns[i] += line[i]
        elif operations[i] == '*':
            columns[i] *= line[i]

After iterating over all rows, columns contains the result for each column. The sum of these results is the answer for part one:

sum(columns)

Part two

The input for today’s puzzle is formatted with irregular whitespace between numbers and operators. This becomes important in part two.

Using the same example input:

123 328  51 64 
 45 64  387 23 
  6 98  215 314
*   +   *   +

Now we need to walk through the rows from right to left, digit by digit. Starting from the rightmost column, we read each column, concatenate the digits from top to bottom to form a number, and continue until we encounter a column where every position is a space. At that point we apply the operator from the bottom row to the numbers we have collected.

In the example, moving right‑to‑left yields the numbers 4, 431, and 623. The operator for this group is +, so we compute 4 + 431 + 623 = 1058.

A reverse iteration over column indices can be done with range():

for i in range(len(problems[0]) - 1, -2, -1):
    ...
  • len(problems[0]) - 1 is the index of the rightmost character.
  • -2 is the stop value (exclusive); we stop after processing index -1.
  • The final -1 makes the loop step backwards.

The full iteration logic:

for i in range(len(problems[0]) - 1, -2, -1):
    if all(problem[i] == ' ' for problem in problems) or i == -1:
        operation = operations.pop()
        if operation == '+':
            total += sum(numbers)
        elif operation == '*':
            total += prod(numbers)   # requires `from math import prod`
        numbers = []
    else:
        column = ''.join(problem[i] for problem in problems).strip()
        numbers.append(int(column))

total accumulates the result for part two.

Back to Blog

Related posts

Read more »

Monkey Market

Part 1 Another math gauntlet I get to program a bunch of math operations. Some will be part of several conditionals. I've done it before. I'm confident I can d...

Advent of Code 2025 - December 8th

In this series, I'll share my progress with the 2025 version of Advent of Code. Check the first post for a short intro to this series. You can also follow my pr...

Flatten a Nested List

Hey everyone! 👋 I know I've been a bit quiet lately. I actually came down with a pretty bad flu last week, which completely knocked me out. 🤒 That's why I mis...

12 Days of Shell

Article URL: https://12days.cmdchallenge.com Comments URL: https://news.ycombinator.com/item?id=46190577 Points: 31 Comments: 8...