diff --git a/README.md b/README.md index 52863a7..2c994de 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ https://adventofcode.com/2024 | 03 | Mull It Over | :pushpin: | :pushpin: | Regex | | 04 | Ceres Search | :pushpin: | :pushpin: | Matrix multidirectional search | | 05 | Print Queue | :pushpin: | :pushpin: | Queue order | -| 06 | | | | | +| 06 | Guard Gallivant | :pushpin: | | 2D navigation | | 07 | | | | | | 08 | | | | | | 09 | | | | | diff --git a/assets/day_6_guard_map_input b/assets/day_6_guard_map_input new file mode 100644 index 0000000..e2b0649 --- /dev/null +++ b/assets/day_6_guard_map_input @@ -0,0 +1,130 @@ +......#........#..........#.................##......................#.............#..................#............#............#.. +.....#............................................#.......................#.......##.................#..#...................#.#... +......#...................#............................#.....#.........#................................................#......... +...........................................#.......#..........#..#......#.........#.#.#.#................#.##........##........... +........#.........................................#.......#.....#..........#...#....##....#..#.......##................#......#... +...................#..#.....................................................#.................................................#... +....#..................#..........#........#.............#................................................................#....... +......#..............#..................#...........#......#.....#...........................#..#.#.....................#.....#... +......#...#...............................................#.....#.........................#..........#............................ +......#......#..........................................#..................................#....#..#................#..##...#..... +........................................................................................#......................................... +................#..........#..........................#.##......................#..........#...................................... +.............#.............................#......#..............................#....................#...........#.....#...#..... +.......#.................#..................#..........................#...........................................#.............. +.......#.....................................................#..........................................#....#.#.....#......#..... +...#......#............#........#......................#...........................#.#....#..#....................#.............## +...........................##..#..#..........#....................#................................................#......#....... +.....................#...#.......................#...#..#.....#................#...........................................#....#. +.................#................#.............#...#...........................................................#..#.#............ +#.....................#....................##.......#...................#..........#..........................#................... +.#..........##......#...#......#................#.....................................#.#.....#..............#.................... +...#...#..#..........................................................#..#..#...................................................... +..................#.#..............#...#...............................#...#....#.#.........#.........................##.......... +.......#.........................#..............................................#........#....#....#.........#.................... +...........#......#......#...........#........#.............................##.................................................#.. +............................#...................................#......#........................................#................. +......................................##...........................................#.............#..#...........#...#.....#....... +...................................................#.................................#............................................ +..........................#..........................................#..#.....#.................#.#......#........................ +..................#...........#.............................#....#.#..................#................................#.......... +...................#....#..#.........................................................#....#....................................... +..#..........................................................##..................................#......................#....#.#.. +#.........................#.....#...............................#........................#................#....................... +......................#......#...#..........#......#..............................#...#......#.................................#.. +...............#.....#............................................................................#............................... +#.....................#........#.#........................................................#.......................#.............#. +.......#...#..........#........#...........#.................#..................#....................................##.....#..... +.#......#..................................#.......................................................................#.............. +.................#.....#...........................................................#.............................................. +.........#....................................^......#..................#..............................#..#.............#...#..... +.............................................................#.....#....................................#......................... +....................#.................#........#........................................................#..#............#........# +.....#..........................................................#...........................#.................#....##............. +..............#.........#.................#............................#.............#......................#.#............#...... +.............#.................#.........................................#...#................#.............#.....#............... +....#.....##..#......#........#............................................#..........#.......#.................................## +..........................................................................#....................................................... +.......#..........#.............................................................................................##..#............. +............................#...............#...............................................................................#..... +....#......#...............................................................................................................#...... +..................................#......................#.....#............................................#.....#........#...... +...............................#..........................................#....................................................... +.....#.............#...#..............#.#............#....................................................................#....... +..............................................#...............................#......##.......#................#..........#.#..... +........#.....##...............................................#.........................#........................................ +.....#.........................#.#........#.............................................#.............#..............##........... +........................................#..##.........................#............#...........#.............#.................... +.....................#.#................#............................#.................#.....#..............................#..... +................#.....................................#..#....................#..................................#................ +...........#...................#.................#..........................................................#.......#...........#. +...........................#..................................#......#..............#............#.........#....#................# +..................................................................................................................#............... +..............#....#.#............................................................................#.....#.........#........#...... +..#...........#.................#.#......#................................#..........................#...........................# +............#...........##...............................#..................................................#..................... +..............##.#..................................................................................#.........#......#......#..... +................................................................#...............#....#....##................#..................... +.........#..#.....................#........................................................#......#................#.............. +...#..........#..........#..................#.......................#........................#...#................#.........#..... +...................................#..#...............................#..........#................................................ +.........................#........................................................................................................ +......................#.........#....................................................##............................#.....#........ +.#....#.....................#.........................................................#....#........#............................. +..........................................#....................................#........................................#......... +.................................................#.......................................#........................................ +#..#...............#......................#....#.............................................#................#........##......... +.........#....#........##.........#.....#.........#...............................................#..................#....#...#... +..#............................................................................#...............................................#.. +.......................................................#.........#......#..........................#.#............................ +.........#.............#.................##.....#.....#.........#............#.........#..................#.....#.............#... +#..........#.........#...............................................................................................#............ +.......................#....................#..................................................#.................................. +.#................#.................................#........................................#...........#........................ +.........#........................................................................................................................ +..................................................................................#..........#.....................#.............. +..#...............................#.......#.....................#......................#.....#..........................#......... +......#....#..........................................................#..........................#................................ +...........................................##..................................................##..................#...#.......... +...............#....#...........................#........................................#..............#....................##... +#......#......................................................#..........#.........................#.........#.................... +...#...................................#......#..........................................#.........................#.............# +#.....#.........#.............#.......................................#...............................#........................... +...................#.#..........................................................................#...............#................. +.......#......##..........#....................................................#..................#..........#.................... +.............................................#.##..............#......................#......#..#..................#..........#... +..##...................#...................................#.........................................#...........#................ +...................................................#.....................................#.......................#...............# +..........#.......##...................#...............#...............................................................#.......... +.......................#............#............##.............................................#......##.................#...#... +.........#.................................................................................#............#....................#.... +.......##.........#..................................................................#................#........................... +................#.............................................................................................#................... +......................#.......#....#.................................................#......#.#...............................#... +.........#.........#.#.....##.#..................................##...................................................#........... +.#......................................................#.......#......##........#..........#.........#....#...................... +........#.#......................#................................................................................................ +..................#.............................#.................#..................#......................#......##.#........... +................##.............................................#....#..........##.#.#....#........................................ +..............#...................#.......#............................#.#.....................................................#.. +............#..............................................#....#.....................#...#.......................#.....#......#.. +..............#........................................................#............................................#............. +.......#.....#.........#..............#..............................#.......................#..#................#....#..#........ +.......................#......#........................................#.................#..........#............................. +...........#..................#.............................#............................................................#........ +......#....#......#.....................#.....................#........#.....................#...#.....#.#...................#.... +#..............................................##...#.....#...........#..........................................#................ +..........#..........#.#........................................#..#....#......................................................... +..................#......................................#...#.......#............................................................ +...#.............................#..................................................#...........#................................. +................................##.#.............#........................#...#...............................................#... +...#...................................#.......................#........................#............#...............#....#.#..... +..............................................................##.......#...............#.........#..................#............. +....#............................#..................................#..#..#...............#..............##....................... +#...#.................##...............................#...........#...........#..............#.#.........#........#...#.......... +..........................#.....#....................#..............#.............#..........#....................#.........#..... +......#.......................................................#........#.....#..........................#...#.........#........... +...............................#............#.....#...........................#...............#........#.......................... +............#..#......#...#..#.....................#...............#.........#...........................................#.....#.. +.......#..#..................#............#...........#..............................................................#............ +....#......#.##..#......##..........#.......#............#...............#....#....................................#..........#.#. diff --git a/src/advent_of_code/fri_06.rs b/src/advent_of_code/fri_06.rs new file mode 100644 index 0000000..4f247a2 --- /dev/null +++ b/src/advent_of_code/fri_06.rs @@ -0,0 +1,73 @@ +use std::collections::HashSet; + +use super::*; + +impl Guard { + fn step(&mut self, map: &FloorMap) -> IsGuardOut { + let mut future_x = self.x; + let mut future_y = self.y; + + match self.dir { + Direction::N => { + if self.y == 0 { + return true; + } + future_y -= 1 + } + Direction::S => { + if self.y == map.len() - 1 { + return true; + } + future_y += 1 + } + Direction::E => { + if self.x == map[0].len() - 1 { + return true; + } + future_x += 1; + } + Direction::W => { + if self.x == 0 { + return true; + } + future_x -= 1; + } + } + + match map[future_y][future_x] { + Floor::Clear => { + self.x = future_x; + self.y = future_y; + } + Floor::Obstacle => { + self.turn_90(); + return self.step(map); + } + } + + false + } + + fn turn_90(&mut self) { + match self.dir { + Direction::N => self.dir = Direction::E, + Direction::S => self.dir = Direction::W, + Direction::E => self.dir = Direction::S, + Direction::W => self.dir = Direction::N, + } + } +} + +pub fn guard_gallivant(input: &str) -> PositionCount { + let (mut the_guard, floor_map) = utils::read_floor_map_and_guard_input(input); + + let mut visited_coordinates: HashSet<(usize, usize)> = HashSet::new(); + + // FIXME: this logic is not good enough, since the guard could walk on the same place twice, the second time should not be counted + visited_coordinates.insert((the_guard.x, the_guard.y)); + while !the_guard.step(&floor_map) { + visited_coordinates.insert((the_guard.x, the_guard.y)); + } + + visited_coordinates.len() +} diff --git a/src/advent_of_code/mod.rs b/src/advent_of_code/mod.rs index 63b12f3..a7ffd5c 100644 --- a/src/advent_of_code/mod.rs +++ b/src/advent_of_code/mod.rs @@ -1,3 +1,4 @@ +mod fri_06; mod mon_02; mod sun_01; mod thu_05; @@ -46,4 +47,8 @@ pub fn historian_hysteria() { thu_05::mid_queue_sum("./assets/day_5_rules_queue_input"); println!("The update mid-queue-sum is {}", queue_mid_sum); println!("The fixed update mid-queue-sum is {}", fixed_queue_mid_sum); + + // Friday 06 + let guard_position_count = fri_06::guard_gallivant("./assets/day_6_guard_map_input"); + println!("The guard will visit {} positions", guard_position_count); } diff --git a/src/advent_of_code/types.rs b/src/advent_of_code/types.rs index 4e36606..b524c29 100644 --- a/src/advent_of_code/types.rs +++ b/src/advent_of_code/types.rs @@ -23,3 +23,23 @@ pub type XMASCount = i32; // Print Queue pub struct Rule(pub u32, pub u32); pub type Queue = Vec; + +// Guard Gallivant +pub enum Direction { + N, + S, + E, + W, +} +pub enum Floor { + Clear, + Obstacle, +} +pub type PositionCount = usize; +pub type IsGuardOut = bool; +pub type FloorMap = Vec>; +pub struct Guard { + pub x: usize, + pub y: usize, + pub dir: Direction, +} diff --git a/src/advent_of_code/utils.rs b/src/advent_of_code/utils.rs index a192a5c..a070977 100644 --- a/src/advent_of_code/utils.rs +++ b/src/advent_of_code/utils.rs @@ -86,6 +86,45 @@ pub fn read_rules_and_queue(input: &str) -> (Vec, Vec) { (rules, queues) } +pub fn read_floor_map_and_guard_input(input: &str) -> (Guard, FloorMap) { + let mut floor_map: FloorMap = vec![]; + let mut the_guard: Guard = Guard { + x: 0, + y: 0, + dir: Direction::N, + }; + + for (line_index, line) in read_to_string(input).unwrap().lines().enumerate() { + let mut line_vec: Vec = vec![]; + for (character_index, character) in line.chars().into_iter().enumerate() { + match character { + '.' => line_vec.push(Floor::Clear), + '#' => line_vec.push(Floor::Obstacle), + '>' | '<' | '^' | 'v' => { + let guard_direction: Direction = { + match character { + '>' => Direction::E, + '<' => Direction::W, + '^' => Direction::N, + 'v' => Direction::S, + _ => Direction::N, + } + }; + the_guard.dir = guard_direction; + the_guard.x = character_index; + the_guard.y = line_index; + line_vec.push(Floor::Clear); + } + _ => line_vec.push(Floor::Obstacle), + } + } + + floor_map.push(line_vec); + } + + (the_guard, floor_map) +} + pub fn calc_distance(num_1: T, num_2: T) -> T where T: PartialOrd + Sub,