@@ -5,6 +5,18 @@ use itertools::Itertools;
5
5
6
6
advent_of_code:: solution!( 19 ) ;
7
7
8
+ fn parse_input ( input : & str ) -> ( Vec < & str > , HashSet < & str > , usize ) {
9
+ let blocks = input. blocks ( ) ;
10
+ let towels = blocks[ 0 ] . split ( ", " ) . collect :: < HashSet < & str > > ( ) ;
11
+ let max_len = towels
12
+ . iter ( )
13
+ . map ( |s| s. len ( ) )
14
+ . max ( )
15
+ . expect ( "There should be a max" ) ;
16
+ let patterns = blocks[ 1 ] . lines ( ) . collect_vec ( ) ;
17
+ ( patterns, towels, max_len)
18
+ }
19
+
8
20
fn is_possible < ' a > (
9
21
s : & ' a str ,
10
22
towels : & HashSet < & ' a str > ,
@@ -35,19 +47,11 @@ fn is_possible<'a>(
35
47
}
36
48
37
49
pub fn part_one ( input : & str ) -> Option < u64 > {
38
- let blocks = input. blocks ( ) ;
39
- let towels = blocks[ 0 ] . split ( ", " ) . collect :: < HashSet < & str > > ( ) ;
40
- let max_len = towels
41
- . iter ( )
42
- . map ( |s| s. len ( ) )
43
- . max ( )
44
- . expect ( "There should be a max" ) ;
45
- let patterns = blocks[ 1 ] . lines ( ) . collect_vec ( ) ;
46
- let mut memo = HashMap :: new ( ) ;
50
+ let ( patterns, towels, max_len) = parse_input ( input) ;
47
51
Some (
48
52
patterns
49
53
. into_iter ( )
50
- . filter ( |s| is_possible ( s, & towels, max_len, & mut memo ) )
54
+ . filter ( |s| is_possible ( s, & towels, max_len, & mut HashMap :: new ( ) ) )
51
55
. count ( ) as u64 ,
52
56
)
53
57
}
@@ -77,19 +81,11 @@ fn possible_ways<'a>(
77
81
}
78
82
79
83
pub fn part_two ( input : & str ) -> Option < u64 > {
80
- let blocks = input. blocks ( ) ;
81
- let towels = blocks[ 0 ] . split ( ", " ) . collect :: < HashSet < & str > > ( ) ;
82
- let max_len = towels
83
- . iter ( )
84
- . map ( |s| s. len ( ) )
85
- . max ( )
86
- . expect ( "There should be a max" ) ;
87
- let patterns = blocks[ 1 ] . lines ( ) . collect_vec ( ) ;
88
- let mut memo = HashMap :: new ( ) ;
84
+ let ( patterns, towels, max_len) = parse_input ( input) ;
89
85
Some (
90
86
patterns
91
87
. into_iter ( )
92
- . map ( |s| possible_ways ( s, & towels, max_len, & mut memo ) )
88
+ . map ( |s| possible_ways ( s, & towels, max_len, & mut HashMap :: new ( ) ) )
93
89
. sum :: < u64 > ( ) ,
94
90
)
95
91
}
0 commit comments