-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path38.py
44 lines (37 loc) · 1.04 KB
/
38.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
# UNSOLVED
from functools import cache
f = open("37sample.in")
towels = []
patterns = []
for line in f.readlines():
if "," in line:
towels = line.strip().split(", ")
continue
if line != "\n":
patterns.append(line.strip())
count = 0
@cache
def good_pattern(pattern, top=True):
global towels
global count
if pattern == "":
return True
if pattern in towels:
return True
found_good_pattern = False
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, False):
is_good_pattern = False
if is_good_pattern and top:
count += 1
if is_good_pattern:
found_good_pattern = True
return found_good_pattern
for pattern in patterns:
good_pattern(pattern)
print(count)