]> aoc.elinar.fr Git - aoc_2024/commitdiff
Day16 - part 1 finally working
authoralex <>
Mon, 16 Dec 2024 20:34:03 +0000 (21:34 +0100)
committeralex <>
Mon, 16 Dec 2024 20:34:03 +0000 (21:34 +0100)
With the help from input maze found on Reddit.
The implementation was not counting the rotation on the current tile but
after. Therefore going backward was seen as cheapest than going forward
at some intersection.

src/day16.rs

index edc65e3761b063319de2edac777409c32fc66166..c743ad04dd0f6e00bb4bfbac4d471ef996b06d89 100644 (file)
@@ -60,16 +60,11 @@ impl Puzzle {
 
         let mut next: Vec<(isize, isize)> = Vec::new();
         next.push(self.start);
-        println!("start={:?}, end={:?}", self.start, self.end);
         while let Some(pos) = next.pop() {
-            if pos == self.end {
-                break;
-            }
             let tile = *score.get(&pos).unwrap();
             let score_curr = tile.score;
             let dir_curr = tile.dir;
             let dir_opp = (-dir_curr.0, -dir_curr.1);
-            //println!(" pos={:?}  score={:?}", pos, score_curr);
             [(-1, 0), (0, 1), (1, 0), (0, -1)].into_iter()
                 .filter(|d| *d != dir_opp)
                 .filter(|d| self.map.contains_key(&(pos.0 + d.0, pos.1 + d.1)))
@@ -81,8 +76,13 @@ impl Puzzle {
                             score_next += 1000;
                         }
                         let tile_next = score.get_mut(&pos_next).unwrap();
-                        if score_next < tile_next.score {
+                        // the rotation is counted on the next tile
+                        // check that the score at the tile will contain a rotation or not
+                        if score_next < tile_next.score || (score_next < tile_next.score + 1000 && dir != tile_next.dir) {
                             tile_next.score = score_next;
+                            if score_next > tile_next.score {
+                                tile_next.score -= 1000;
+                            }
                             tile_next.dir = dir;
                             next.push(pos_next);
                         }
@@ -94,7 +94,6 @@ impl Puzzle {
                 let t2 = score.get(b).unwrap();
                 t2.score.cmp(&t1.score)
             });
-            //println!("  next={:?}", next);
         }
 
         score.get(&self.end).unwrap().score
@@ -213,6 +212,18 @@ mod tests {
 #....................#.............................#
 #S...................#.............................#
 ####################################################";
+    // another one from
+    // <https://old.reddit.com/r/adventofcode/comments/1hfmbel/2024_day_16_part_1_c_stuck_in_part_1/m2cjv60/>
+    // this one helped spot the bug in the implementation
+    // especially around the (1,4) position
+    static TEXT_INPUT_4: &str = "\
+##########
+#.......E#
+#.##.#####
+#..#.....#
+##.#####.#
+#S.......#
+##########";
 
     #[test]
     fn test_part1() {
@@ -220,6 +231,7 @@ mod tests {
         assert_eq!(11048, run_part1(TEXT_INPUT_1).unwrap());
         assert_eq!(21148, run_part1(TEXT_INPUT_2).unwrap());
         assert_eq!(5078, run_part1(TEXT_INPUT_3).unwrap());
+        assert_eq!(4013, run_part1(TEXT_INPUT_4).unwrap());
     }
 
     #[test]