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
46
47
48
49
|
from fileinput import input
from itertools import takewhile
inp = map(str.strip, input())
towels = [t.strip() for t in next(takewhile(bool, inp)).split(",")]
designs = [d for d in inp if d]
# Construct trie
trie = {}
for towel in towels:
prev = None
curr = trie
for t in towel:
term, succ = curr.setdefault(t, (False, {}))
prev = curr
curr = succ
prev[towel[-1]] = (True, curr)
def drops(design, start=0):
curr = trie
for n, s in enumerate(design[start:], start=start + 1):
if s not in curr:
return
term, curr = curr[s]
if term:
yield n
def match(design):
q = list(drops(design))
seen = set()
while q:
n = q.pop()
if n == len(design):
return True
if n in seen:
continue
seen.add(n)
for d in drops(design, n):
q.append(d)
return False
silver = sum(map(match, designs))
gold = 0
print("silver:", silver)
print("gold:", gold)
|