1
1
use crate :: data:: load;
2
2
use itertools:: Itertools ;
3
- use std:: { fmt:: Display , num:: ParseIntError } ;
3
+ use std:: { fmt:: Display , iter :: zip , num:: ParseIntError } ;
4
4
use thiserror:: Error ;
5
5
6
6
#[ derive( Error , Debug , PartialEq , Eq ) ]
@@ -85,24 +85,29 @@ impl Display for Row {
85
85
}
86
86
}
87
87
88
+ fn _split_condition_vec ( conditions : & [ Condition ] ) -> Vec < & [ Condition ] > {
89
+ conditions
90
+ . split ( |x| x == & Condition :: Operational )
91
+ . filter ( |cs| !cs. is_empty ( ) )
92
+ . collect :: < Vec < _ > > ( )
93
+ }
94
+
88
95
impl Row {
89
96
fn _final_validation ( & self , prop_conds : & [ Condition ] ) -> bool {
90
- let s = display_vec ( prop_conds) ;
91
- let splits = s. split ( '.' ) . filter ( |s| !s. is_empty ( ) ) . collect :: < Vec < _ > > ( ) ;
97
+ let splits = _split_condition_vec ( prop_conds) ;
92
98
if self . groups . len ( ) != splits. len ( ) {
93
99
return false ;
94
100
}
95
- for ( i , seq ) in splits. iter ( ) . enumerate ( ) {
96
- if seq. len ( ) != self . groups [ i ] {
101
+ for ( seq , gr ) in zip ( splits. iter ( ) , self . groups . iter ( ) ) {
102
+ if seq. len ( ) != * gr {
97
103
return false ;
98
104
}
99
105
}
100
106
true
101
107
}
102
108
103
109
fn _in_progress_validation ( & self , prop_conds : & [ Condition ] ) -> bool {
104
- let s = display_vec ( prop_conds) ;
105
- let splits = s. split ( '.' ) . filter ( |s| !s. is_empty ( ) ) . collect :: < Vec < _ > > ( ) ;
110
+ let splits = _split_condition_vec ( prop_conds) ;
106
111
if self . groups . len ( ) < splits. len ( ) {
107
112
return false ;
108
113
}
@@ -152,9 +157,6 @@ impl Row {
152
157
}
153
158
prop_conds = self . prune ( & prop_conds) ;
154
159
}
155
- for cond in prop_conds. iter ( ) {
156
- log:: debug!( " {}" , display_vec( cond) ) ;
157
- }
158
160
prop_conds. len ( )
159
161
}
160
162
}
@@ -172,6 +174,33 @@ pub fn puzzle_1(input: &str) -> Result<usize, PuzzleErr> {
172
174
Ok ( rows. into_iter ( ) . map ( |r| r. num_solutions ( ) ) . sum ( ) )
173
175
}
174
176
177
+ pub fn puzzle_2 ( input : & str ) -> Result < usize , PuzzleErr > {
178
+ let rows = parse_input ( input) ?
179
+ . iter ( )
180
+ . map ( |r| {
181
+ let mut c = ( 0 ..5 )
182
+ . map ( |_| {
183
+ let mut new_c = r. conditions . clone ( ) ;
184
+ new_c. push ( Condition :: Unknown ) ;
185
+ new_c
186
+ } )
187
+ . concat ( ) ;
188
+ let _ = c. pop ( ) ;
189
+ let g = ( 0 ..5 ) . map ( |_| r. groups . clone ( ) ) . concat ( ) ;
190
+ Row {
191
+ conditions : c,
192
+ groups : g,
193
+ }
194
+ } )
195
+ . collect :: < Vec < Row > > ( ) ;
196
+
197
+ Ok ( rows. into_iter ( ) . map ( |r| r. num_solutions ( ) ) . sum ( ) )
198
+ // for row in rows.iter() {
199
+ // log::debug!("{}", row);
200
+ // }
201
+ // Ok(0)
202
+ }
203
+
175
204
pub fn main ( data_dir : & str ) {
176
205
println ! ( "Day 12: Hot Springs" ) ;
177
206
let data = load ( data_dir, 12 , None ) ;
@@ -185,10 +214,10 @@ pub fn main(data_dir: &str) {
185
214
assert_eq ! ( answer_1, Ok ( 7716 ) ) ;
186
215
187
216
// Puzzle 2.
188
- // let answer_2 = puzzle_2(&data);
189
- // match answer_2 {
190
- // Ok(x) => println!(" Puzzle 2: {}", x),
191
- // Err(e) => panic!("No solution to puzzle 2: {}", e),
192
- // }
217
+ let answer_2 = puzzle_2 ( & data) ;
218
+ match answer_2 {
219
+ Ok ( x) => println ! ( " Puzzle 2: {}" , x) ,
220
+ Err ( e) => panic ! ( "No solution to puzzle 2: {}" , e) ,
221
+ }
193
222
// assert_eq!(answer_2, Ok(933))
194
223
}
0 commit comments