-
Notifications
You must be signed in to change notification settings - Fork 0
/
19.py
38 lines (29 loc) · 776 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
# pylint: skip-file
# mypy: ignore-errors
# flake8: noqa
from functools import cache
input_value = open("19.txt", "r").read()
[towels, designs] = input_value.split("\n\n")
towels = set(towels.split(", "))
designs = designs.split("\n")
@cache
def ways_possible(suffix):
if len(suffix) == 0:
return 1
ways_here = 0
for end in range(1, len(suffix) + 1):
needed_towel = suffix[:end]
if needed_towel in towels:
ways_here += ways_possible(suffix[end:])
return ways_here
is_possible = 0
total_ways_possible = 0
for design in designs:
ways_here = ways_possible(design)
total_ways_possible += ways_here
if ways_here > 0:
is_possible += 1
# Part 1:
print(is_possible)
# Part 2:
print(total_ways_possible)