2024 day 19.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Mikael CAPELLE
2024-12-19 08:12:17 +01:00
parent 146d025d41
commit 683cac334c
3 changed files with 448 additions and 1 deletions

View File

@@ -1,7 +1,42 @@
from functools import cache
from typing import Any, Iterator
from ..base import BaseSolver
@cache
def is_valid(design: str, towels: tuple[str, ...]) -> bool:
if not design:
return True
return any(
design.startswith(towel) and is_valid(design[len(towel) :], towels)
for towel in towels
)
@cache
def count_valid(design: str, towels: tuple[str, ...]) -> int:
if not design:
return 1
return sum(
design.startswith(towel) and count_valid(design[len(towel) :], towels)
for towel in towels
)
class Solver(BaseSolver):
def solve(self, input: str) -> Iterator[Any]: ...
def solve(self, input: str) -> Iterator[Any]:
towels_s, designs_s = input.split("\n\n")
towels = tuple(s.strip() for s in towels_s.split(","))
designs = [
design
for design in self.progress.wrap(designs_s.splitlines())
if is_valid(design, towels)
]
yield len(designs)
yield sum(count_valid(design, towels) for design in self.progress.wrap(designs))