@@ -12,7 +12,10 @@ const MAX_ANTENNA_PER_TYPE: usize = 16;
1212type AntennaMap < ' a > =
1313 HeaplessHashMap < & ' a char , HeaplessVec < Point , MAX_ANTENNA_PER_TYPE > , MAX_ANTENNA_TYPES > ;
1414
15- fn solve < const PART1 : bool > ( input : & str ) -> Option < u32 > {
15+ fn solve (
16+ input : & str ,
17+ update_set : & mut impl FnMut ( & Grid < char > , & mut HeaplessHashSet < Point , 2048 > , ( Point , Point ) ) ,
18+ ) -> Option < u32 > {
1619 let grid = Grid :: new_char_grid_from_str ( input) ;
1720
1821 let antennas: AntennaMap = into_group_map_heapless (
@@ -24,33 +27,36 @@ fn solve<const PART1: bool>(input: &str) -> Option<u32> {
2427 let mut pos_set: HeaplessHashSet < Point , 2048 > = HeaplessHashSet :: new ( ) ;
2528 for ( _, vec) in antennas. iter ( ) {
2629 for ( a, b) in vec. iter ( ) . tuple_combinations ( ) {
27- for ( a, b) in [ ( a, b) , ( b, a) ] {
28- let dir = b. as_vector_direction ( a) ;
29- let mut p = * a + dir;
30- if PART1 {
31- if grid. is_in_bounds ( p) {
32- pos_set. insert ( p) . unwrap ( ) ;
33- }
34- } else {
35- pos_set. insert ( * a) . unwrap ( ) ;
36- pos_set. insert ( * b) . unwrap ( ) ;
37- while grid. is_in_bounds ( p) {
38- pos_set. insert ( p) . unwrap ( ) ;
39- p = p + dir;
40- }
41- }
30+ for ( & a, & b) in [ ( a, b) , ( b, a) ] {
31+ update_set ( & grid, & mut pos_set, ( a, b) ) ;
4232 }
4333 }
4434 }
4535
4636 Some ( pos_set. len ( ) as u32 )
4737}
38+
4839pub fn part_one ( input : & str ) -> Option < u32 > {
49- solve :: < true > ( input)
40+ solve ( input, & mut |grid, pos_set, ( a, b) | {
41+ let dir = b. as_vector_direction ( & a) ;
42+ let p = a + dir;
43+ if grid. is_in_bounds ( p) {
44+ pos_set. insert ( p) . unwrap ( ) ;
45+ }
46+ } )
5047}
5148
5249pub fn part_two ( input : & str ) -> Option < u32 > {
53- solve :: < false > ( input)
50+ solve ( input, & mut |grid, pos_set, ( a, b) | {
51+ let dir = b. as_vector_direction ( & a) ;
52+ let mut p = a + dir;
53+ pos_set. insert ( a) . unwrap ( ) ;
54+ pos_set. insert ( b) . unwrap ( ) ;
55+ while grid. is_in_bounds ( p) {
56+ pos_set. insert ( p) . unwrap ( ) ;
57+ p = p + dir;
58+ }
59+ } )
5460}
5561
5662#[ cfg( test) ]
0 commit comments