-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19.py
49 lines (36 loc) · 887 Bytes
/
19.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
45
46
47
48
49
import functools
def getdata():
patterns = None
designs = []
for i in open("data/19.txt"):
if patterns is None:
patterns = i.strip().split(", ")
continue
elif i.strip() != "":
designs.append(i.strip())
return patterns, designs
patterns, designs = getdata()
@functools.cache
def dfs(d):
if d == "":
return True
for p in patterns:
if d.startswith(p):
if dfs(d[len(p) :]):
return True
return False
@functools.cache
def dfswithcount(d):
if d == "":
return 1
res = 0
for p in patterns:
if d.startswith(p):
res += dfswithcount(d[len(p) :])
return res
def part1():
print("Part 1 is", sum([dfs(d) for d in designs]))
def part2():
print("Part 2 is", sum([dfswithcount(d) for d in designs]))
part1()
part2()