# ========== 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]] linesLen = len(lines) lineLen = len(lines[0]) grandTotal = 0 for i in range(lineLen): if(lines[4][i] == "*"): grandTotal = grandTotal + (lines[0][i]*lines[1][i]*lines[2][i]*lines[3][i]) elif(lines[4][i] == "+"): grandTotal = grandTotal + (lines[0][i]+lines[1][i]+lines[2][i]+lines[3][i]) else: print("ohoh ein Fehler") # ========== PART 2 ========== with open("day6/input.txt") as file: raw_lines = [line.rstrip('\n') for line in file.readlines()] 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}")