@@ -4,6 +4,7 @@ use crate::utils::point::Point;
44use itertools:: Itertools ;
55
66const ROLL_OF_PAPER : char = '@' ;
7+ const REMOVED : char = 'X' ;
78
89pub struct Day04 ;
910
@@ -27,22 +28,21 @@ impl Solution for Day04 {
2728 fn part_two ( & self , input : & str ) -> String {
2829 let mut grid: Grid < char > = Grid :: from ( input) ;
2930
30- let mut removed_count: u32 = 0 ;
31- for roll in grid. get_all_positions ( & ROLL_OF_PAPER ) {
32- Self :: try_to_remove ( & mut grid, & roll, & mut removed_count) ;
33- }
34-
35- removed_count. to_string ( )
31+ grid. get_all_positions ( & ROLL_OF_PAPER )
32+ . iter ( )
33+ . fold ( 0 , |acc, roll| acc + Self :: try_to_remove ( & mut grid, roll) )
34+ . to_string ( )
3635 }
3736}
3837
3938impl Day04 {
40- fn try_to_remove ( grid : & mut Grid < char > , roll : & Point , removed_count : & mut u32 ) {
39+ fn try_to_remove ( grid : & mut Grid < char > , roll : & Point ) -> u32 {
40+ let mut removed_count = 0u32 ;
4141 if grid
4242 . get_for_point ( roll)
4343 . is_some_and ( |e| * e != ROLL_OF_PAPER )
4444 {
45- return ;
45+ return removed_count ;
4646 }
4747
4848 let adjacent = roll. adjacent_with_diagonal_vectors ( ) ;
@@ -52,13 +52,15 @@ impl Day04 {
5252 . collect_vec ( ) ;
5353
5454 if adjacent_rolls. len ( ) < 4 {
55- grid. modify ( * roll, 'X' ) ;
56- * removed_count += 1 ;
55+ grid. modify ( * roll, REMOVED ) ;
56+ removed_count += 1 ;
5757
5858 for adj_roll in adjacent_rolls {
59- Self :: try_to_remove ( grid, & adj_roll. position ( ) , removed_count ) ;
59+ removed_count += Self :: try_to_remove ( grid, & adj_roll. position ( ) ) ;
6060 }
6161 }
62+
63+ removed_count
6264 }
6365}
6466
0 commit comments