Don't need to push visited position twice...
authorGreg Burri <greg.burri@gmail.com>
Mon, 12 Dec 2022 09:47:58 +0000 (10:47 +0100)
committerGreg Burri <greg.burri@gmail.com>
Mon, 12 Dec 2022 09:47:58 +0000 (10:47 +0100)
src/day12.rs

index 1e03ed3..ee08a24 100644 (file)
@@ -52,6 +52,8 @@ pub fn nb_steps(hm: &Heightmap, path: Path) -> i32 {
         positions
     };
 
+    visited[hm.end.0][hm.end.1] = 0;
+
     loop {
         step = step + 1;
         for (i, j) in positions.drain(..) {
@@ -59,7 +61,6 @@ pub fn nb_steps(hm: &Heightmap, path: Path) -> i32 {
                 return step;
             }
 
-            visited[i][j] = step;
             for (ni, nj) in neighbors(i, j) {
                 if visited[ni][nj] == -1 && hm.elevations[i][j] - hm.elevations[ni][nj] <= 1 {
                     visited[ni][nj] = step;