Files
advent-of-code-2025/day6/part2.py

33 lines
827 B
Python

from itertools import groupby
from math import prod
from pathlib import Path
FILE = "input.txt"
def main(file: str = FILE) -> int:
*value_rows, operator_row = Path(file).read_text().splitlines()
operators = operator_row.split()
# Columns represent vertical numbers; blank columns separate problems.
columns = ("".join(col).strip() for col in zip(*value_rows))
operands = (
[int(n) for n in group]
for is_value, group in groupby(columns, key=bool)
if is_value
)
return sum(compute(vals, op) for vals, op in zip(operands, operators))
def compute(vals: list[int], op: str) -> int:
ops = {"+": sum, "*": prod}
try:
return ops[op](vals)
except KeyError:
raise ValueError(f"unknown operator: {op}")
if __name__ == "__main__":
print(main())