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)))
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);
}
let t2 = score.get(b).unwrap();
t2.score.cmp(&t1.score)
});
- //println!(" next={:?}", next);
}
score.get(&self.end).unwrap().score
#....................#.............................#
#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() {
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]