Skip to content

Commit 8365a4d

Browse files
committed
AoC 2024 Day 19 Part 2 [too slow]
1 parent 2f66bfe commit 8365a4d

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

src/main/python/AoC2024_19.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from aoc.common import InputData
99
from aoc.common import SolutionBase
1010
from aoc.common import aoc_samples
11+
from aoc.common import log
1112

1213
Input = tuple[set[str], list[str]]
1314
Output1 = int
@@ -52,12 +53,39 @@ def find(w: str, pos: int) -> bool:
5253
return ans
5354

5455
def part_2(self, input: Input) -> Output2:
55-
return 0
56+
towels, designs = input
57+
possible = set[str](towels)
58+
59+
def find(w: str, pos: tuple[int], poss: set[tuple[str, ...]]) -> None:
60+
if sum(pos) == len(w):
61+
ii = 0
62+
lst = list[str]()
63+
for i in range(1, len(pos)):
64+
lst.append(w[ii:ii+pos[i]])
65+
ii += pos[i]
66+
if all(_ in towels for _ in lst):
67+
poss.add(tuple(_ for _ in lst))
68+
return
69+
pp = [p for p in possible if w[sum(pos):].startswith(p)]
70+
for ppp in pp:
71+
possible.add(w[:sum(pos) + len(ppp)])
72+
tmp = list(pos[:]) + [len(ppp)]
73+
new_pos = tuple(_ for _ in tmp)
74+
find(w, new_pos, poss)
75+
76+
ans = 0
77+
for design in designs:
78+
log(f"{design=}")
79+
poss = set[tuple[str, ...]]()
80+
find(design, (0, ), poss)
81+
log(f"{design=}: {len(poss)}: {poss}")
82+
ans += len(poss)
83+
return ans
5684

5785
@aoc_samples(
5886
(
5987
("part_1", TEST, 6),
60-
# ("part_2", TEST, "TODO"),
88+
("part_2", TEST, 16),
6189
)
6290
)
6391
def samples(self) -> None:

0 commit comments

Comments
 (0)