File tree 1 file changed +13
-8
lines changed
1 file changed +13
-8
lines changed Original file line number Diff line number Diff line change
1
+ import collections
1
2
import functools
2
3
3
4
from . import CombinedRunner
4
5
5
6
6
- def parse_input (data : str ) -> tuple [tuple [str , ... ], list [str ]]:
7
+ def parse_input (data : str ) -> tuple [list [str ], list [str ]]:
7
8
patterns , designs = data .strip ().split ("\n \n " )
8
9
9
- return tuple ( patterns .split (", " ) ), designs .split ("\n " )
10
+ return patterns .split (", " ), designs .split ("\n " )
10
11
11
12
12
13
class DayRunner (CombinedRunner ):
13
14
@classmethod
14
15
def run_both (cls , input : str ) -> int :
15
16
patterns , designs = parse_input (input )
16
17
18
+ by_prefix = collections .defaultdict (list )
19
+ for prefix in patterns :
20
+ by_prefix [prefix [0 ]].append (prefix )
21
+
17
22
possible = 0
18
23
ways = 0
19
24
20
25
@functools .cache
21
26
def is_possible (design : str ) -> bool :
22
27
if not design :
23
28
return 1
24
-
25
- return sum (
26
- is_possible (design [len (pat ) :])
27
- for pat in patterns
28
- if design .startswith (pat )
29
- )
29
+ else :
30
+ return sum (
31
+ is_possible (design [len (prefix ) :])
32
+ for prefix in by_prefix [ design [ 0 ]]
33
+ if design .startswith (prefix )
34
+ )
30
35
31
36
for design in designs :
32
37
if (solve := is_possible (design )) > 0 :
You can’t perform that action at this time.
0 commit comments