import math text: str with open("input.txt") as f: text = f.read() total: int = 0 def calc(lines: list[str], start: int, stop: int) -> int: numbers: list[int] = [] for i in range(start, stop): number: int = 0 for line in lines[:-1]: if (line[i] != ' '): number = number * 10 + int(line[i]) numbers.append(number) operator = lines[-1][start] if (operator == "+"): return sum(numbers) return math.prod(numbers) lines: list[str] = text.splitlines() prev = len(lines[-1]) for i in range(len(lines[-1]) - 1, -1, -1): operator: str = lines[-1][i] if (operator in "+*"): total += calc(lines, i, prev) prev = i - 1 print(total)