Advent of Code 2025 - Day 6
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]) - 1is the index of the rightmost character.-2is the stop value (exclusive); we stop after processing index-1.- The final
-1makes 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.