-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday19.py
More file actions
executable file
·54 lines (38 loc) · 1.2 KB
/
day19.py
File metadata and controls
executable file
·54 lines (38 loc) · 1.2 KB
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
50
51
52
53
54
from functools import cache
from aoc_utils import * # type: ignore
from aocd import get_data
data = get_data(year=2024, day=19, block=True)
@cache
def find_towels(options, target):
for option in options:
if option == target[: len(option)]:
rest = target[len(option) :]
if rest == "":
return [option]
elif m := find_towels(options, rest):
return [option] + m
return []
@cache
def count_arrangements(options, target):
count = 0
for option in options:
if option == target[: len(option)]:
rest = target[len(option) :]
if rest == "":
count += 1
else:
count += count_arrangements(options, rest)
return count
def parse(data):
ts, ds = data.split("\n\n")
towels = tuple(ts.split(", "))
designs = ds.splitlines()
return towels, designs
def part_one(data):
towels, designs = parse(data)
return sum(1 for design in designs if find_towels(towels, design))
def part_two(data):
towels, designs = parse(data)
return sum(count_arrangements(towels, design) for design in designs)
print(part_one(data))
print(part_two(data))