Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/year_2025/day_6/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Day 6: Trash Compactor

## Problem Description

After helping the Elves in the kitchen, you were taking a break and helping them re-enact a movie scene when you over-enthusiastically jumped into the garbage chute!

A brief fall later, you find yourself in a garbage smasher. Unfortunately, the door's been magnetically sealed.

As you try to find a way out, you are approached by a family of cephalopods! They're pretty sure they can get the door open, but it will take some time. While you wait, they're curious if you can help the youngest cephalopod with her math homework.

## Cephalopod Math

Cephalopod math doesn't look that different from normal math. The math worksheet (your puzzle input) consists of a list of problems; each problem has a group of numbers that need to be either added (`+`) or multiplied (`*`) together.

However, the problems are arranged a little strangely; they seem to be presented next to each other in a very long horizontal list. For example:

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

Each problem's numbers are arranged vertically; at the bottom of the problem is the symbol for the operation that needs to be performed. Problems are separated by a full column of only spaces. The left/right alignment of numbers within each problem can be ignored.

So, this worksheet contains four problems:

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

To check their work, cephalopod students are given the grand total of adding together all of the answers to the individual problems. In this worksheet, the grand total is `33210 + 490 + 4243455 + 401 = 4277556`.

Of course, the actual worksheet is much wider. You'll need to make sure to unroll it completely so that you can read the problems clearly.

## Challenge

Solve the problems on the math worksheet. What is the grand total found by adding together all of the answers to the individual problems?

---

## Part Two

The big cephalopods come back to check on how things are going. When they see that your grand total doesn't match the one expected by the worksheet, they realize they forgot to explain how to read cephalopod math.

Cephalopod math is written **right-to-left in columns**. Each number is given in its own column, with the most significant digit at the top and the least significant digit at the bottom. (Problems are still separated with a column consisting only of spaces, and the symbol at the bottom of the problem is still the operator to use.)

Here's the example worksheet again:

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

Reading the problems right-to-left one column at a time, the problems are now quite different:

- The rightmost problem is `4 + 431 + 623 = 1058`
- The second problem from the right is `175 * 581 * 32 = 3253600`
- The third problem from the right is `8 + 248 + 369 = 625`
- Finally, the leftmost problem is `356 * 24 * 1 = 8544`

Now, the grand total is `1058 + 3253600 + 625 + 8544 = 3263827`.

### Challenge Part Two

Solve the problems on the math worksheet again. What is the grand total found by adding together all of the answers to the individual problems?
Empty file added src/year_2025/day_6/__init__.py
Empty file.
165 changes: 165 additions & 0 deletions src/year_2025/day_6/code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import os


def solve_worksheet(file_name, part_two=False):
"""
Advent of Code 2025 - Day 6: Trash Compactor
Parse cephalopod math worksheet and calculate the grand total.

Part 1: Read numbers horizontally (left-to-right rows)
Part 2: Read numbers vertically in columns (right-to-left, top-to-bottom)
"""
current_dir = os.path.dirname(os.path.abspath(__file__))
file_path = os.path.join(current_dir, file_name)

with open(file_path, "r") as file:
lines = file.read().splitlines()

# Parse the worksheet
# Remove empty lines
data_rows = [line for line in lines if line.strip()]

if not data_rows:
return 0

# Last row contains operators
operator_row = data_rows[-1]
number_rows = data_rows[:-1]

# Pad all rows to the same length
max_length = max(len(row) for row in data_rows)
padded_rows = [row.ljust(max_length) for row in data_rows]
operator_row = padded_rows[-1]
number_rows = padded_rows[:-1]

if part_two:
# Part 2: Read right-to-left in columns
# Each number is in its own column, read top-to-bottom
problems = []
col = 0

while col < max_length:
# Skip columns that are all spaces
if all(row[col] == " " for row in padded_rows):
col += 1
continue

# Found the start of a problem - collect all columns until we hit all spaces
problem_start = col
problem_end = col

# Extend to include all adjacent non-all-space columns
while problem_end < max_length:
if all(row[problem_end] == " " for row in padded_rows):
break
problem_end += 1

# Extract the operator for this problem
operator_text = operator_row[problem_start:problem_end].strip()
operator = None
for char in operator_text:
if char in ["+", "*"]:
operator = char
break

# Extract numbers by reading each column right-to-left, top-to-bottom
numbers = []
for c in range(problem_end - 1, problem_start - 1, -1): # Right-to-left
# Read this column top-to-bottom to form a number
digits = []
for row in number_rows:
if c < len(row) and row[c].isdigit():
digits.append(row[c])

if digits:
# Combine digits top-to-bottom to form number
number = int("".join(digits))
numbers.append(number)

if operator and numbers:
problems.append((operator, numbers))

col = problem_end

else:
# Part 1: Read left-to-right horizontally (original logic)
# Identify problem columns by finding where operators are
# and grouping adjacent non-space columns
problems = []
col = 0

while col < max_length:
# Skip columns that are all spaces
if all(row[col] == " " for row in padded_rows):
col += 1
continue

# Found the start of a problem - collect all columns until we hit all spaces
problem_start = col
problem_end = col

# Extend to include all adjacent non-all-space columns
while problem_end < max_length:
if all(row[problem_end] == " " for row in padded_rows):
break
problem_end += 1

# Extract the problem from this column range
problem_text = [row[problem_start:problem_end] for row in number_rows]
operator_text = operator_row[problem_start:problem_end].strip()

# Find the operator
operator = None
for char in operator_text:
if char in ["+", "*"]:
operator = char
break

# Extract numbers from the problem text
numbers = []
for row_text in problem_text:
# Extract all numbers from this row
row_text = row_text.strip()
if row_text and row_text.replace(" ", "").isdigit():
# Could be one or more numbers separated by spaces
parts = row_text.split()
for part in parts:
if part.isdigit():
numbers.append(int(part))

if operator and numbers:
problems.append((operator, numbers))

col = problem_end

# Calculate answers for each problem
grand_total = 0

for operator, numbers in problems:
if operator == "+":
result = sum(numbers)
else: # operator == '*'
result = 1
for num in numbers:
result *= num

grand_total += result

return grand_total


if __name__ == "__main__":
# Test with example
test_result = solve_worksheet("test.txt")
print(f"Part 1 Test result: {test_result}")

# Solve with actual input
result = solve_worksheet("input.txt")
print(f"Part 1 result: {result}")

# Part 2
test_result_2 = solve_worksheet("test.txt", True)
print(f"Part 2 Test result: {test_result_2}")

result_2 = solve_worksheet("input.txt", True)
print(f"Part 2 result: {result_2}")
Loading