This commit is contained in:
@@ -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))
|
||||
|
Reference in New Issue
Block a user