@@ -19,27 +19,52 @@ pub fn input_generator(input: &str) -> Vec<Calibration> {
1919 . map ( |n| n. parse ( ) . unwrap ( ) )
2020 . collect ( ) ,
2121 } ) ;
22- acc
22+ ancc
2323 } )
2424}
2525
26- pub fn test_calibration ( current : usize , target : usize , index : usize , params : & [ usize ] ) -> bool {
26+ pub fn test_calibration (
27+ current : usize ,
28+ target : usize ,
29+ index : usize ,
30+ params : & [ usize ] ,
31+ part2 : bool ,
32+ ) -> bool {
2733 let index = index + 1 ;
2834 if index >= params. len ( ) {
2935 if current == target {
3036 return true ;
3137 }
3238 return false ;
3339 }
34- test_calibration ( current * params[ index] , target, index, params)
35- || test_calibration ( current + params[ index] , target, index, params)
40+ test_calibration ( current * params[ index] , target, index, params, part2)
41+ || test_calibration ( current + params[ index] , target, index, params, part2)
42+ || ( part2
43+ && test_calibration (
44+ current * 10_usize . pow ( params[ index] . to_string ( ) . len ( ) as u32 ) + params[ index] ,
45+ target,
46+ index,
47+ params,
48+ part2,
49+ ) )
3650}
3751
3852#[ aoc( day7, part1) ]
3953pub fn part1 ( input : & [ Calibration ] ) -> usize {
4054 let mut sum = 0 ;
4155 for c in input {
42- if test_calibration ( c. numbers [ 0 ] , c. value , 0 , & c. numbers ) {
56+ if test_calibration ( c. numbers [ 0 ] , c. value , 0 , & c. numbers , false ) {
57+ sum += c. value ;
58+ }
59+ }
60+ sum
61+ }
62+
63+ #[ aoc( day7, part2) ]
64+ pub fn part2 ( input : & [ Calibration ] ) -> usize {
65+ let mut sum = 0 ;
66+ for c in input {
67+ if test_calibration ( c. numbers [ 0 ] , c. value , 0 , & c. numbers , true ) {
4368 sum += c. value ;
4469 }
4570 }
@@ -62,11 +87,11 @@ mod tests {
6287
6388 #[ test]
6489 fn test_part1 ( ) {
65- assert_eq ! ( 1 , part1( & input_generator( INPUT ) ) )
90+ assert_eq ! ( 3749 , part1( & input_generator( INPUT ) ) )
6691 }
6792
68- // #[test]
69- // fn test_part2() {
70- // assert_eq!(0 , part2(&input_generator(INPUT)))
71- // }
93+ #[ test]
94+ fn test_part2 ( ) {
95+ assert_eq ! ( 11387 , part2( & input_generator( INPUT ) ) )
96+ }
7297}
0 commit comments