From 000001505b2873db7e13420a30bd05d7ea73ae17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Jane=C5=BEi=C4=8D?= Date: Sun, 7 Dec 2025 06:45:25 +0100 Subject: [PATCH] feat: add solution 2025/07 --- data/example/2025/07.txt | 16 +++++++++ src/solution/year_2025/day_07.py | 56 ++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 data/example/2025/07.txt create mode 100644 src/solution/year_2025/day_07.py diff --git a/data/example/2025/07.txt b/data/example/2025/07.txt new file mode 100644 index 000000000..57a2466 --- /dev/null +++ b/data/example/2025/07.txt @@ -0,0 +1,16 @@ +.......S....... +............... +.......^....... +............... +......^.^...... +............... +.....^.^.^..... +............... +....^.^...^.... +............... +...^.^...^.^... +............... +..^...^.....^.. +............... +.^.^.^.^.^...^. +............... diff --git a/src/solution/year_2025/day_07.py b/src/solution/year_2025/day_07.py new file mode 100644 index 000000000..e744851 --- /dev/null +++ b/src/solution/year_2025/day_07.py @@ -0,0 +1,56 @@ +from functools import lru_cache +from typing import Any + + +def part_1(input_data: str) -> Any: + lines = input_data.splitlines() + first, rest = lines[0], lines[1:] + + s = {i for i, x in enumerate(first) if x == "S"} + ns: set[int] = set() + + c = 0 + for line in rest: + ns.clear() + + while s: + n = s.pop() + if line[n] == "^": + ns.add(n - 1) + ns.add(n + 1) + c += 1 + else: + ns.add(n) + + s.update(ns) + + return c + + +def trace_path(n: int, i: int, lines: list[str]) -> int: + @lru_cache(None) + def aux(n: int, i: int) -> int: + for idx, line in filter(lambda x: x[0] >= i, enumerate(lines)): + if line[n] == "^": + left = aux(n - 1, idx + 1) + right = aux(n + 1, idx + 1) + return 1 + left + right + + return 0 + + return aux(n, i) + + +def part_2(input_data: str) -> Any: + lines = input_data.splitlines() + first, rest = lines[0], lines[1:] + n = next(filter(lambda x: x[1] == "S", enumerate(first)))[0] + return 1 + trace_path(n, 0, rest) + + +def test_part_1(example_data): + assert part_1(example_data) == 21 + + +def test_part_2(example_data): + assert part_2(example_data) == 40