From 00000240ea5dcfc40da35d5f3e97a364e50c82bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Jane=C5=BEi=C4=8D?= Date: Fri, 12 Dec 2025 13:01:13 +0100 Subject: [PATCH] feat: add solution 2025/11 --- data/example/2025/11.txt | 15 ++++++ src/solution/year_2025/day_11.py | 86 ++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 data/example/2025/11.txt create mode 100644 src/solution/year_2025/day_11.py diff --git a/data/example/2025/11.txt b/data/example/2025/11.txt new file mode 100644 index 000000000..f03555a --- /dev/null +++ b/data/example/2025/11.txt @@ -0,0 +1,15 @@ +aaa: you hhh +you: bbb ccc +bbb: ddd eee +ccc: ddd eee fff +ddd: ggg +eee: out +fff: out +ggg: out +hhh: ccc fff iii +iii: out +svr: zzz www +zzz: dac +dac: www out fft +fft: out +www: out diff --git a/src/solution/year_2025/day_11.py b/src/solution/year_2025/day_11.py new file mode 100644 index 000000000..9c896e3 --- /dev/null +++ b/src/solution/year_2025/day_11.py @@ -0,0 +1,86 @@ +import collections +import functools +from typing import Any + + +def _parse_input(input_data: str) -> tuple[dict[str, int], dict[int, list[int]]]: + counter = 0 + ids = {} + + graph = collections.defaultdict(list) + + for line in input_data.splitlines(): + node, _neighs = line.split(": ") + neighs = _neighs.split() + + if node not in ids: + ids[node] = counter + counter += 1 + + for n in filter(lambda x: x not in ids, neighs): + ids[n] = counter + counter += 1 + + for n in neighs: + graph[ids[n]].append(ids[node]) + + return ids, graph + + +def _count_paths( + graph: dict[int, list[int]], + start: int, + end: int, + skip: set[int] | None = None, +) -> int: + if skip is None: + skip = set() + + @functools.cache + def traverse(node: int): + if node in skip: + return 0 + if node == start: + return 1 + return sum(traverse(n) for n in graph[node]) + + return traverse(end) + + +def part_1(input_data: str) -> Any: + ids, graph = _parse_input(input_data) + print(graph) + + you = ids["you"] + out = ids["out"] + + return _count_paths(graph, you, out) + + +def part_2(input_data: str) -> Any: + ids, graph = _parse_input(input_data) + + svr = ids["svr"] + out = ids["out"] + + dac = ids["dac"] + fft = ids["fft"] + + svr_dac = _count_paths(graph, svr, dac, {out, fft}) + svr_fft = _count_paths(graph, svr, fft, {out, dac}) + + dac_fft = _count_paths(graph, dac, fft, {out}) + fft_dac = _count_paths(graph, fft, dac, {out}) + + dac_out = _count_paths(graph, dac, out, {fft}) + fft_out = _count_paths(graph, fft, out, {dac}) + + return svr_dac * dac_fft * fft_out + svr_fft * fft_dac * dac_out + + +def test_part_1(example_data): + assert part_1(example_data) == 5 + + +def test_part_2(example_data): + assert part_2(example_data) == 1