@@ -5,19 +5,45 @@ pub struct Calibration {
55 value : usize ,
66 numbers : Vec < usize > ,
77}
8+
89#[ aoc_generator( day7) ]
9- pub fn input_generator ( _input : & str ) -> Vec < Calibration > {
10- // input.lines().fold(vec![], |mut reports, l| {
11- // reports.push(l.trim().split(" ").map(|s| s.parse().unwrap()).collect());
12- // reports
13- // })
14- vec ! [ ]
10+ pub fn input_generator ( input : & str ) -> Vec < Calibration > {
11+ input. lines ( ) . fold ( vec ! [ ] , |mut acc, l| {
12+ let tmp = l. split_once ( ":" ) . unwrap ( ) ;
13+ acc. push ( Calibration {
14+ value : tmp. 0 . parse ( ) . unwrap ( ) ,
15+ numbers : tmp
16+ . 1
17+ . trim ( )
18+ . split ( " " )
19+ . map ( |n| n. parse ( ) . unwrap ( ) )
20+ . collect ( ) ,
21+ } ) ;
22+ acc
23+ } )
24+ }
25+
26+ pub fn test_calibration ( current : usize , target : usize , index : usize , params : & [ usize ] ) -> bool {
27+ let index = index + 1 ;
28+ if index >= params. len ( ) {
29+ if current == target {
30+ return true ;
31+ }
32+ return false ;
33+ }
34+ test_calibration ( current * params[ index] , target, index, params)
35+ || test_calibration ( current + params[ index] , target, index, params)
1536}
1637
1738#[ aoc( day7, part1) ]
1839pub fn part1 ( input : & [ Calibration ] ) -> usize {
19- dbg ! ( input) ;
20- 0
40+ let mut sum = 0 ;
41+ for c in input {
42+ if test_calibration ( c. numbers [ 0 ] , c. value , 0 , & c. numbers ) {
43+ sum += c. value ;
44+ }
45+ }
46+ sum
2147}
2248
2349#[ cfg( test) ]
@@ -36,7 +62,7 @@ mod tests {
3662
3763 #[ test]
3864 fn test_part1 ( ) {
39- assert_eq ! ( 0 , part1( & input_generator( INPUT ) ) )
65+ assert_eq ! ( 1 , part1( & input_generator( INPUT ) ) )
4066 }
4167
4268 // #[test]
0 commit comments