1- use std:: collections:: HashSet ;
21use aoc_runner_derive:: { aoc, aoc_generator} ;
2+ use std:: collections:: HashSet ;
33
44#[ aoc_generator( day10) ]
55pub fn input_generator ( input : & str ) -> Vec < Vec < usize > > {
@@ -9,19 +9,29 @@ pub fn input_generator(input: &str) -> Vec<Vec<usize>> {
99 . enumerate ( )
1010 . map ( |( y, s) | {
1111 s. chars ( )
12- . enumerate ( )
12+ . enumerate ( )
1313 . map ( |( x, l) | {
14- if l == '0' {
15- starts. push ( ( y, x) )
16- } l. to_digit ( 10 ) . unwrap ( ) as usize
17- } )
14+ if l == '0' {
15+ starts. push ( ( y, x) )
16+ }
17+ l. to_digit ( 10 ) . unwrap ( ) as usize
18+ } )
1819 . collect ( )
1920 } )
2021 . collect ( )
2122}
2223
23- pub fn hike ( input : & [ Vec < usize > ] , pos : ( usize , usize ) , current : usize , visited : & mut HashSet < ( usize , usize ) > ) -> usize {
24+ pub fn hike (
25+ input : & [ Vec < usize > ] ,
26+ pos : ( usize , usize ) ,
27+ current : usize ,
28+ part2 : bool ,
29+ visited : & mut HashSet < ( usize , usize ) > ,
30+ ) -> usize {
2431 if current == 9 {
32+ if part2 {
33+ return 1 ;
34+ }
2535 if !visited. contains ( & ( pos. 0 , pos. 1 ) ) {
2636 visited. insert ( ( pos. 0 , pos. 1 ) ) ;
2737 return 1 ;
@@ -32,41 +42,57 @@ pub fn hike(input: &[Vec<usize>], pos: (usize, usize), current: usize, visited:
3242
3343 let directions = [
3444 ( 0 , -1 ) , // left,
35- ( 0 , 1 ) , // right,
45+ ( 0 , 1 ) , // right,
3646 ( -1 , 0 ) , // up,
37- ( 1 , 0 ) , // down,
47+ ( 1 , 0 ) , // down,
3848 ] ;
39-
49+
4050 for ( dy, dx) in directions {
4151 let ny = pos. 0 as isize + dy;
4252 let nx = pos. 1 as isize + dx;
4353 if ny >= 0
44- && ny < input. len ( ) as isize
45- && nx >= 0
46- && nx < input[ pos. 0 ] . len ( ) as isize
47- && input[ ny as usize ] [ nx as usize ] == current + 1 {
48- path += hike ( input, ( ny as usize , nx as usize ) , current+1 , visited) ;
54+ && ny < input. len ( ) as isize
55+ && nx >= 0
56+ && nx < input[ pos. 0 ] . len ( ) as isize
57+ && input[ ny as usize ] [ nx as usize ] == current + 1
58+ {
59+ path += hike (
60+ input,
61+ ( ny as usize , nx as usize ) ,
62+ current + 1 ,
63+ part2,
64+ visited,
65+ ) ;
4966 }
50-
5167 }
5268 path
5369}
5470
5571#[ aoc( day10, part1) ]
5672pub fn part1 ( input : & [ Vec < usize > ] ) -> usize {
5773 let mut score = 0 ;
58- let mut count = 0 ;
5974 for ( y, row) in input. iter ( ) . enumerate ( ) {
6075 for ( x, col) in row. iter ( ) . enumerate ( ) {
6176 if * col == 0 {
62- let mut visited: HashSet < ( usize , usize ) > = HashSet :: new ( ) ;
63- score += hike ( input, ( y, x) , 0 , & mut visited) ;
64- count +=1 ;
65- dbg ! ( count, score) ;
77+ let mut visited: HashSet < ( usize , usize ) > = HashSet :: new ( ) ;
78+ score += hike ( input, ( y, x) , 0 , false , & mut visited) ;
79+ }
80+ }
81+ }
82+ score
83+ }
84+
85+ #[ aoc( day10, part2) ]
86+ pub fn part2 ( input : & [ Vec < usize > ] ) -> usize {
87+ let mut score = 0 ;
88+ for ( y, row) in input. iter ( ) . enumerate ( ) {
89+ for ( x, col) in row. iter ( ) . enumerate ( ) {
90+ if * col == 0 {
91+ let mut visited: HashSet < ( usize , usize ) > = HashSet :: new ( ) ;
92+ score += hike ( input, ( y, x) , 0 , true , & mut visited) ;
6693 }
6794 }
6895 }
69- dbg ! ( count, score) ;
7096 score
7197}
7298
@@ -87,4 +113,9 @@ mod tests {
87113 fn test_part1 ( ) {
88114 assert_eq ! ( 36 , part1( & input_generator( INPUT ) ) )
89115 }
116+
117+ #[ test]
118+ fn test_part2 ( ) {
119+ assert_eq ! ( 81 , part2( & input_generator( INPUT ) ) )
120+ }
90121}
0 commit comments