advent-of-code/2024/19/two.py
2024-12-25 12:59:49 +01:00

33 lines
651 B
Python

#!/usr/bin/env python3
import sys
import functools
input_file = sys.argv[1]
with open(input_file) as fd:
lines = [line.rstrip() for line in fd.readlines()]
towels = set(map(str.strip, lines[0].split(",")))
patterns = lines[2:]
@functools.cache
def possible(pattern: str) -> int:
if not pattern:
return 1
possible_count = 0
for towel in towels:
if pattern.startswith(towel):
possible_count += possible(pattern[len(towel) :])
return possible_count
possible_count = 0
for pattern in patterns:
res = possible(pattern)
print(27, pattern, res)
possible_count += res
print(possible_count)