-
Notifications
You must be signed in to change notification settings - Fork 0
/
202419.py
45 lines (35 loc) · 979 Bytes
/
202419.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from collections import defaultdict
from functools import cache
from typing import DefaultDict, List
from utils import input_as_strings_iter
input_part_one = True
patterns: List[str] = []
towels: DefaultDict[str, List[str]] = defaultdict(list)
for line in input_as_strings_iter("202419.txt"):
if line == "":
input_part_one = False
continue
if input_part_one:
for t in line.split(", "):
t = t.strip()
towels[t[0]].append(t)
else:
patterns.append(line)
@cache
def possible(pattern: str) -> int:
if pattern == "":
return 1
candidates = []
for t in towels[pattern[0]]:
if pattern.startswith(t):
candidates.append(pattern[len(t) :])
return sum(possible(c) for c in candidates)
part_one = 0
part_two = 0
for p in patterns:
c = possible(p)
if c > 0:
part_one += 1
part_two += c
print(f"Part one: {part_one}")
print(f"Part two: {part_two}")