Advent of Code 2025 – Day 6
Today reminds me of how I did math in primary school. You know, putting two or more numbers below each other and then going from right to left, from digit to digit, to either add or multiply. Life was easy and peaceful back then. Much has changed since.
But I digress. Let’s look at the puzzles.
Check out my full solution for day 6 at GitHub.
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, only not all numbers in one row, but all numbers in one column.
For example:
123 328 51 64
45 64 387 23
6 98 215 314
* + * +
The result of the first column is 123 * 45 * 6, the result of the second column is 328 + 64 + 98, etcetera. We can solve this programatically 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, I’ve already added the first line into the columns variable, so I have a starting value for each column to begin with. That’s why the for-loop starts at index position 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 columns, the variable columns contains the result for each column. The sum of this result is the answer for part one.
sum(columns)
Part two
Without realising it at first, the input for today’s puzzle is formatted in a specific way. There is whitespace between numbers and operators, but it is not homogenous. The number of spaces between numbers and operators differ, for reasons that become clear in part two. Let’s check the example input again:
123 328 51 64
45 64 387 23
6 98 215 314
* + * +
Instead of going from left to right and applying the operation from the last line to a column, we now need to walk through the rows from right to left, and digit by digit. Starting from the right, we need to read each column, generate a number from all digits in that column, until we reach a column where each position is a space. When we reach that space, we know we can apply the operation to the numbers we collected.
In the example, going from right to left, column by column (and from top to bottom), we end up with the numbers 4 (empty + empty + 4), 431 (4 + 3 + 1) and 623 (6 + 2 + 3). In the last column before the space in the last row, we see that the operator is +, meaning we need to add these numbers together: 4 + 431 + 623 = 1058.
In Python, we can use the range() function in combination with a for-loop to start at some value, decreasing a step in each iteration and continue up until some end value:
for i in range(len(problems[0]) - 1, -2, -1)
This for-loop says to start at a number that is equal to the length of the first row of numbers (problems[0]). As this length is the same for each row, we can essentially use any row to determine the start value. We subtract one because we’re working with index positions in an array. The next number -2 is the end value (not including). I end the iteration at -1 instead of 0 to make sure that also the last operation at index position 0 is executed. The final number -1 means we subtract one in each step of the iteration.
Now we can simply walk through all columns with digits simultaneously from right to left. If we reach a column where all “digits” are empty spaces, or if we’ve reached the end (index -1), we know we can execute the next operation. Otherwise, we append all digits in the current column, cast the resulting string into an integer and add it to a list of numbers. This is the list of numbers we apply the operation to when we reach that empty space column.
The full iteration in code:
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)
numbers = []
else:
column = ''.join(problem[i] for problem in problems).strip()
numbers.append(int(column))
The total we keep increasing within this for-loop is the result for part two.