-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path37.py
39 lines (33 loc) · 923 Bytes
/
37.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
from functools import cache
f = open("37.in")
towels = []
patterns = []
for line in f.readlines():
if "," in line:
towels = line.strip().split(", ")
continue
if line != "\n":
patterns.append(line.strip())
@cache
def good_pattern(pattern):
global towels
if pattern == "":
return True
if pattern in towels:
return True
for towel in towels:
if towel in pattern:
# Can only split the first instance to make sure all possibilites are covered
remaining = pattern.split(towel, 1)
is_good_pattern = True
for segment in remaining:
if not good_pattern(segment):
is_good_pattern = False
if is_good_pattern:
return True
return False
count = 0
for pattern in patterns:
if good_pattern(pattern):
count += 1
print(count)