diff --git a/data/example/2025/06.txt b/data/example/2025/06.txt new file mode 100644 index 000000000..337b837 --- /dev/null +++ b/data/example/2025/06.txt @@ -0,0 +1,4 @@ +123 328 51 64 + 45 64 387 23 + 6 98 215 314 +* + * + diff --git a/src/solution/year_2025/day_06.py b/src/solution/year_2025/day_06.py new file mode 100644 index 000000000..e409948 --- /dev/null +++ b/src/solution/year_2025/day_06.py @@ -0,0 +1,52 @@ +import functools +import math +from typing import Any + + +def part_1(input_data: str) -> Any: + lines = input_data.splitlines() + numbers = [list(map(int, line.split())) for line in lines[:-1]] + signs = lines[-1].split() + + c = 0 + for i, x in enumerate(signs): + acc = [n[i] for n in numbers] + c += sum(acc) if x == "+" else math.prod(acc) + + return c + + +def part_2(input_data: str) -> Any: + lines = input_data.splitlines() + numbers = [[int(s) if s != " " else None for s in line] for line in lines[:-1]] + signs = [s if s != " " else None for s in lines[-1]] + + idx = len(signs) - 1 + c = 0 + + while idx >= 0: + acc = [] + + while True: + _digits = [n[idx] for n in numbers] + digits = [n for n in _digits if n is not None] + acc.append(functools.reduce(lambda acc, x: acc * 10 + x, digits)) + + sign = signs[idx] + + if sign is not None: + c += sum(acc) if sign == "+" else math.prod(acc) + idx -= 2 + break + + idx -= 1 + + return c + + +def test_part_1(example_data): + assert part_1(example_data) == 4277556 + + +def test_part_2(example_data): + assert part_2(example_data) == 3263827