32 lines
776 B
Python
32 lines
776 B
Python
from math import prod
|
|
from pathlib import Path
|
|
|
|
FILE = "input.txt"
|
|
|
|
def main():
|
|
raw_lines = Path(FILE).read_text().splitlines()
|
|
operators = raw_lines[-1].split()
|
|
operands: zip[tuple[str]] = zip(*raw_lines[:-1])
|
|
|
|
vals: list[int] = []
|
|
sum = 0
|
|
for val in operands:
|
|
if all(v == ' ' or v == '' for v in val):
|
|
sum += compute(vals, operators.pop(0))
|
|
vals = []
|
|
continue
|
|
vals.append(int("".join(val).strip()))
|
|
return sum + compute(vals, operators.pop(0))
|
|
|
|
def compute(vals: list[int], op: str) -> int:
|
|
if op == '+':
|
|
return sum(vals)
|
|
elif op == '*':
|
|
return prod(vals)
|
|
else:
|
|
print(f"unknown operator: {op}")
|
|
return 0
|
|
|
|
if __name__ == "__main__":
|
|
print(main())
|