From 39c894486f31cc56dcac879ad0bb4f1513e1f81b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20T=C3=B3th?= Date: Sun, 7 Dec 2025 17:38:21 +0100 Subject: [PATCH] Day 6: Part 2 --- day6/main.py | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/day6/main.py b/day6/main.py index 1d5e86b..af12fba 100644 --- a/day6/main.py +++ b/day6/main.py @@ -1,7 +1,7 @@ +# ========== PART 1 ========== with open("day6/input.txt") as file: lines = file.readlines() - lines = [line.strip("\n").split() for line in lines] for i in range(4): lines[i] = [int(number) for number in lines[i]] @@ -18,7 +18,47 @@ for i in range(lineLen): else: print("ohoh ein Fehler") -newGrandTotal = 0 -# Part 2 +# ========== PART 2 ========== +with open("day6/input.txt") as file: + raw_lines = [line.rstrip('\n') for line in file.readlines()] -print(f"Part 1: {grandTotal}\n\rPart 2: {newGrandTotal}") \ No newline at end of file +max_width = max(len(line) for line in raw_lines) +grid = [list(line.ljust(max_width)) for line in raw_lines] + +num_rows = len(grid) - 1 # Number of rows with digits +op_row = len(grid) - 1 # Index of the operator row +max_cols = max_width + + +newGrandTotal = 0 +current_numbers = [] # Stores numbers for the current problem (read R->L) + +for col in range(max_cols - 1, -1, -1): + number_digits = "" + for row in range(num_rows): + char = grid[row][col] + if char.isdigit(): + number_digits += char + + operator = grid[op_row][col] + + if operator == '+' or operator == '*': + if number_digits: + current_numbers.insert(0, int(number_digits)) # Prepend, as we are reading R->L + if not current_numbers: + continue + result = current_numbers[0] + if operator == '*': + for num in current_numbers[1:]: + result *= num + elif operator == '+': + for num in current_numbers[1:]: + result += num + newGrandTotal += result + current_numbers = [] + + elif number_digits: + current_numbers.insert(0, int(number_digits)) + + +print(f"Part 1: {grandTotal}\nPart 2: {newGrandTotal}") \ No newline at end of file