day 19 part 2
parent
f1ed65a6d9
commit
d6b6bb979e
|
@ -0,0 +1,31 @@
|
|||
from functools import cache
|
||||
def solve(file):
|
||||
designs = file.readline().strip().split(", ")
|
||||
assert file.readline() == '\n'
|
||||
|
||||
ds = set(designs)
|
||||
|
||||
@cache
|
||||
def ways(x):
|
||||
n = 0
|
||||
if x in ds:
|
||||
n += 1
|
||||
if len(x) > 1:
|
||||
for i in range(1,len(x)):
|
||||
if x[:i] in ds:
|
||||
n += ways(x[i:])
|
||||
#print("ways(%s) = %s"%(x,n))
|
||||
return n
|
||||
|
||||
t = 0
|
||||
for line in file:
|
||||
towel = line.strip()
|
||||
w = ways(towel)
|
||||
#print(w, towel)
|
||||
t += w
|
||||
|
||||
print(t)
|
||||
|
||||
|
||||
solve(open('sample1.in'))
|
||||
solve(open('input'))
|
Loading…
Reference in New Issue