@@ -8,29 +8,17 @@ pub struct Day06;
88
99impl Solution for Day06 {
1010 fn part_one ( & self , input : & str ) -> String {
11- let mut grid = Grid :: filled (
12- SurfaceRange :: from ( ( Point :: new ( 0 , 0 ) , Point :: new ( 999 , 999 ) ) ) ,
13- 0 ,
14- ) ;
15- let instructions = self . parse ( input) ;
16-
17- for instruction in instructions {
18- instruction. apply_part_one ( & mut grid) ;
19- }
11+ let apply =
12+ |instruction : & dyn Instruction , grid : & mut Grid < u64 > | instruction. apply_part_one ( grid) ;
13+ let grid = self . apply_instructions ( input, apply) ;
2014
2115 grid. get_all_positions ( & 1 ) . len ( ) . to_string ( )
2216 }
2317
2418 fn part_two ( & self , input : & str ) -> String {
25- let mut grid = Grid :: filled (
26- SurfaceRange :: from ( ( Point :: new ( 0 , 0 ) , Point :: new ( 999 , 999 ) ) ) ,
27- 0 ,
28- ) ;
29- let instructions = self . parse ( input) ;
30-
31- for instruction in instructions {
32- instruction. apply_part_two ( & mut grid) ;
33- }
19+ let apply =
20+ |instruction : & dyn Instruction , grid : & mut Grid < u64 > | instruction. apply_part_two ( grid) ;
21+ let grid = self . apply_instructions ( input, apply) ;
3422
3523 grid. all ( ) . values ( ) . sum :: < u64 > ( ) . to_string ( )
3624 }
@@ -59,6 +47,22 @@ impl Day06 {
5947 fn parse_points ( & self , from_str : & str , to_str : & str ) -> ( Point , Point ) {
6048 ( from_str. parse ( ) . unwrap ( ) , to_str. parse ( ) . unwrap ( ) )
6149 }
50+
51+ fn apply_instructions < F > ( & self , input : & str , mut func : F ) -> Grid < u64 >
52+ where
53+ F : FnMut ( & dyn Instruction , & mut Grid < u64 > ) ,
54+ {
55+ let mut grid = Grid :: filled (
56+ SurfaceRange :: from ( ( Point :: new ( 0 , 0 ) , Point :: new ( 999 , 999 ) ) ) ,
57+ 0 ,
58+ ) ;
59+
60+ for instruction in self . parse ( input) {
61+ func ( instruction. as_ref ( ) , & mut grid) ;
62+ }
63+
64+ grid
65+ }
6266}
6367
6468trait Instruction : Debug {
0 commit comments