You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.0 KiB
47 lines
1.0 KiB
#!/usr/bin/env python3
|
|
|
|
import sys
|
|
|
|
def memo(f):
|
|
memo = {}
|
|
|
|
def f2(x,y):
|
|
if x not in memo: memo[x] = f(x,y)
|
|
return memo[x]
|
|
return f2
|
|
|
|
@memo
|
|
def count(p, ts):
|
|
poss = 0
|
|
for t in [t for t in ts if p.startswith(t)]:
|
|
if t == p: poss += 1
|
|
poss += count(p[len(t):],ts)
|
|
return poss
|
|
|
|
|
|
def possible(p, ts):
|
|
poss = False
|
|
for t in [t for t in ts if p.startswith(t)]:
|
|
if t == p: return True
|
|
poss = poss or possible(p[len(t):],ts)
|
|
if poss: return True
|
|
return poss
|
|
|
|
|
|
if __name__ == '__main__':
|
|
towels, patterns = open(sys.argv[1]).read().split('\n\n')
|
|
towels = [t for t in towels.strip('\n').split(', ')]
|
|
patterns = patterns.strip('\n').split('\n')
|
|
|
|
res1, res2 = 0, 0
|
|
for pattern in patterns:
|
|
res1 += possible(pattern, towels)
|
|
if possible(pattern, towels):
|
|
res2 += count(pattern, towels)
|
|
|
|
# challenge 1
|
|
print(f"challenge 1:\n{res1}\n")
|
|
|
|
# challenge 2
|
|
print(f"challenge 2:\n{res2}")
|
|
|
|
|