Skip to content

Commit d4aad95

Browse files
committed
Implement 2024 day 19
1 parent caa2c9b commit d4aad95

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

2024/src/aoc/days/day19.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import functools
2+
3+
from . import CombinedRunner
4+
5+
6+
def parse_input(data: str) -> tuple[tuple[str, ...], list[str]]:
7+
patterns, designs = data.strip().split("\n\n")
8+
9+
return tuple(patterns.split(", ")), designs.split("\n")
10+
11+
12+
@functools.cache
13+
def is_possible(design: str, patterns: tuple[str, ...]) -> bool:
14+
if not design:
15+
return 1
16+
17+
return sum(
18+
is_possible(design[len(pat) :], patterns)
19+
for pat in patterns
20+
if design.startswith(pat)
21+
)
22+
23+
24+
class DayRunner(CombinedRunner):
25+
@classmethod
26+
def run_both(cls, input: str) -> int:
27+
patterns, designs = parse_input(input)
28+
29+
possible = 0
30+
ways = 0
31+
32+
for design in designs:
33+
if (solve := is_possible(design, patterns)) > 0:
34+
possible += 1
35+
ways += solve
36+
37+
return possible, ways

2024/tests/samples/19.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
r, wr, b, g, bwu, rb, gb, br
2+
3+
brwrr
4+
bggr
5+
gbbr
6+
rrbgbr
7+
ubwu
8+
bwurrg
9+
brgr
10+
bbrgwb

2024/tests/test_day19.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from aoc.days.day19 import DayRunner
2+
3+
from . import get_data
4+
5+
6+
def test_sample_part1() -> None:
7+
assert DayRunner.part1(get_data(19)) == 6
8+
9+
10+
def test_sample_part2() -> None:
11+
assert DayRunner.part2(get_data(19)) == 16

0 commit comments

Comments
 (0)