1
- use std:: collections:: HashSet ;
2
-
3
1
use crate :: aoc2024:: Aoc2024 ;
4
2
use crate :: grid:: Grid ;
5
3
use crate :: traits:: days:: Day6 ;
@@ -70,14 +68,15 @@ const DIRECTIONS: [(isize, isize); 4] = [(0, -1), (1, 0), (0, 1), (-1, 0)];
70
68
fn compute_path_size ( grid : & Grid < bool > , sx : usize , sy : usize ) -> Option < usize > {
71
69
let ( mut x, mut y) = ( sx, sy) ;
72
70
let mut didx = 0 ;
73
- let mut positions = HashSet :: new ( ) ;
74
- let mut pos_and_dir = HashSet :: new ( ) ;
71
+ let mut positions = Grid :: new ( grid. width , grid. height , [ false ; 4 ] ) ;
75
72
76
73
loop {
77
- positions. insert ( ( x, y) ) ;
78
- if !pos_and_dir . insert ( ( x , y , didx) ) {
74
+ let pos = positions. get_mut ( x, y) ;
75
+ if pos [ didx] {
79
76
return None ;
80
77
}
78
+ pos[ didx] = true ;
79
+
81
80
let ( dx, dy) = DIRECTIONS [ didx] ;
82
81
if let Some ( ( nx, ny) ) = offset_pair ( grid, x, y, dx, dy) {
83
82
if * grid. get ( nx, ny) {
@@ -91,9 +90,15 @@ fn compute_path_size(grid: &Grid<bool>, sx: usize, sy: usize) -> Option<usize> {
91
90
}
92
91
}
93
92
94
- Some ( positions. len ( ) )
93
+ Some (
94
+ positions
95
+ . iter ( )
96
+ . filter ( |( _, _, & v) | v. into_iter ( ) . any ( |b| b) )
97
+ . count ( ) ,
98
+ )
95
99
}
96
100
101
+ #[ inline]
97
102
fn offset_pair (
98
103
grid : & Grid < bool > ,
99
104
x : usize ,
@@ -110,6 +115,7 @@ fn offset_pair(
110
115
}
111
116
}
112
117
118
+ #[ inline]
113
119
fn offset ( base : usize , offset : isize , max : usize ) -> Option < usize > {
114
120
let res = base as isize + offset;
115
121
0 commit comments