@@ -12,7 +12,10 @@ const MAX_ANTENNA_PER_TYPE: usize = 16;
12
12
type AntennaMap < ' a > =
13
13
HeaplessHashMap < & ' a char , HeaplessVec < Point , MAX_ANTENNA_PER_TYPE > , MAX_ANTENNA_TYPES > ;
14
14
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 > {
16
19
let grid = Grid :: new_char_grid_from_str ( input) ;
17
20
18
21
let antennas: AntennaMap = into_group_map_heapless (
@@ -24,33 +27,36 @@ fn solve<const PART1: bool>(input: &str) -> Option<u32> {
24
27
let mut pos_set: HeaplessHashSet < Point , 2048 > = HeaplessHashSet :: new ( ) ;
25
28
for ( _, vec) in antennas. iter ( ) {
26
29
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) ) ;
42
32
}
43
33
}
44
34
}
45
35
46
36
Some ( pos_set. len ( ) as u32 )
47
37
}
38
+
48
39
pub 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
+ } )
50
47
}
51
48
52
49
pub 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
+ } )
54
60
}
55
61
56
62
#[ cfg( test) ]
0 commit comments