advent-of-code/2024/19/one.py

29 lines
573 B
Python
Raw Normal View History

2024-12-25 12:58:02 +01:00
#!/usr/bin/env python3
import sys
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:]
def possible(pattern: str) -> bool:
if not pattern:
return True
for towel in towels:
if pattern.startswith(towel):
if possible(pattern[len(towel):]):
return True
return False
possible_count = 0
for pattern in patterns:
if possible(pattern):
possible_count += 1
print(possible_count)