Created
December 23, 2024 19:53
-
-
Save rodrigogiraoserrao/d7b646a2fd66d117fd98168d977cd553 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# === Parsing === | |
with open("input.txt", "r") as f: | |
towels = tuple(f.readline().strip().split(", ")) | |
_ = f.readline() | |
patterns = [line.strip() for line in f] | |
# === Part 1 === | |
def build_pattern(pattern, segments): | |
if not pattern: | |
return True | |
return any( | |
build_pattern(pattern.removeprefix(segment), segments) | |
for segment in segments | |
if pattern.startswith(segment) | |
) | |
print(sum(build_pattern(pattern, towels) for pattern in patterns)) | |
# === Part 2 === | |
from functools import cache | |
@cache | |
def count_pattern(pattern, segments): | |
if not pattern: | |
return 1 | |
return sum( | |
count_pattern(pattern.removeprefix(segment), segments) | |
for segment in segments | |
if pattern.startswith(segment) | |
) | |
print(sum(count_pattern(pattern, towels) for pattern in patterns)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment