From bd2d0e077bbb16980133165fe6bd186e0b547c6b Mon Sep 17 00:00:00 2001 From: alex <> Date: Mon, 16 Dec 2024 21:34:03 +0100 Subject: [PATCH] Day16 - part 1 finally working 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 | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/day16.rs b/src/day16.rs index edc65e3..c743ad0 100644 --- a/src/day16.rs +++ b/src/day16.rs @@ -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 + // + // 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] -- 2.39.5