import math text: str with open("input.txt") as f: text = f.read() total: int = 0 lines: list[str] = text.splitlines() numbers: list[list[int]] = [[int(nb_str) for nb_str in line.split(" ") if len(nb_str)] for line in lines[:-1]] for i, operator in enumerate([operator for operator in lines[-1].split(" ") if len(operator)]): if (operator == "+"): total += sum([numbers[j][i] for j in range(len(lines) - 1)]) elif (operator == "*"): total += math.prod([numbers[j][i] for j in range(len(lines) - 1)]) print(total)