Advent of Code solutions

Rename Grid::<T impl Eq>::cursor_at_tile

bwc9876.dev 1cdbe7fc 8b114d23

verified
+10 -5
+6 -1
utils/src/grid.rs
··· 576 576 .find_map(|(p, t)| if t == tile { Some(p) } else { None }) 577 577 } 578 578 579 - pub fn cursor_at<D: Movement>(&self, tile: &T, dir: D) -> Option<GridCursor<T, D>> { 579 + pub fn cursor_at_tile<D: Movement>(&self, tile: &T, dir: D) -> Option<GridCursor<T, D>> { 580 580 self.find_tile(tile).map(|pos| self.cursor(pos, dir)) 581 581 } 582 582 } ··· 831 831 /// Create a new cursor at the given position and direction. 832 832 pub fn new(grid: &'a Grid<T>, pos: Position, dir: D) -> Self { 833 833 Self { grid, pos, dir } 834 + } 835 + 836 + /// Get the current state of the cursor (position and direction) for use in tracking 837 + pub fn state(&self) -> (Position, D) { 838 + (self.pos, self.dir) 834 839 } 835 840 836 841 /// Move the cursor forward one step in the direction it is facing.
+4 -4
years/2024/src/day_6.rs
··· 24 24 let mut visited = HashSet::new(); 25 25 26 26 while let Some((pos, tile)) = curs.peek_forward() { 27 - visited.insert((curs.pos, curs.dir)); 27 + visited.insert(curs.state()); 28 28 if tile.is_open() && pos != obs_pos { 29 29 curs.move_forward(); 30 30 } else { 31 31 curs.turn(true); 32 32 } 33 33 34 - if visited.contains(&(curs.pos, curs.dir)) { 34 + if visited.contains(&curs.state()) { 35 35 return true; 36 36 } 37 37 } ··· 44 44 45 45 fn part_1(input: Self::Input) -> Option<String> { 46 46 let mut curs = input 47 - .cursor_at(&Tile::GuardStart, Direction::North) 47 + .cursor_at_tile(&Tile::GuardStart, Direction::North) 48 48 .unwrap(); 49 49 50 50 let mut visited = HashSet::with_capacity(input.width() * input.height()); ··· 66 66 67 67 fn part_2(input: Self::Input) -> Option<String> { 68 68 let mut curs = input 69 - .cursor_at(&Tile::GuardStart, Direction::North) 69 + .cursor_at_tile(&Tile::GuardStart, Direction::North) 70 70 .unwrap(); 71 71 72 72 let start_curs = curs;