diff --git a/Cargo.lock b/Cargo.lock index 70407a1..b5923cb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,27 @@ dependencies = [ "utils", ] +[[package]] +name = "approx" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cab112f0a86d568ea0e627cc1d6be74a1e9cd55214684db5561995f6dad897c6" +dependencies = [ + "num-traits", +] + +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + +[[package]] +name = "bytemuck" +version = "1.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b37c88a63ffd85d15b406896cc343916d7cf57838a847b3a6f2ca5d39a5695a" + [[package]] name = "day_1" version = "0.1.0" @@ -61,6 +82,7 @@ dependencies = [ name = "day_6" version = "0.1.0" dependencies = [ + "nalgebra-glm", "solver_base", "utils", ] @@ -73,10 +95,145 @@ dependencies = [ "utils", ] +[[package]] +name = "matrixmultiply" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9380b911e3e96d10c1f415da0876389aaf1b56759054eeb0de7df940c456ba1a" +dependencies = [ + "autocfg", + "rawpointer", +] + +[[package]] +name = "nalgebra" +version = "0.33.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26aecdf64b707efd1310e3544d709c5c0ac61c13756046aaaba41be5c4f66a3b" +dependencies = [ + "approx", + "matrixmultiply", + "num-complex", + "num-rational", + "num-traits", + "simba", + "typenum", +] + +[[package]] +name = "nalgebra-glm" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e441f43bccdf40cb6bd4294321e6983c5bc7b9886112d19fd4c9813976b117e4" +dependencies = [ + "approx", + "nalgebra", + "num-traits", + "simba", +] + +[[package]] +name = "num-bigint" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" +dependencies = [ + "num-integer", + "num-traits", +] + +[[package]] +name = "num-complex" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" +dependencies = [ + "num-bigint", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + +[[package]] +name = "rawpointer" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3" + +[[package]] +name = "safe_arch" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3460605018fdc9612bce72735cba0d27efbcd9904780d44c7e3a9948f96148a" +dependencies = [ + "bytemuck", +] + +[[package]] +name = "simba" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3a386a501cd104797982c15ae17aafe8b9261315b5d07e3ec803f2ea26be0fa" +dependencies = [ + "approx", + "num-complex", + "num-traits", + "paste", + "wide", +] + [[package]] name = "solver_base" version = "0.1.0" +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + [[package]] name = "utils" version = "0.1.0" + +[[package]] +name = "wide" +version = "0.7.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58e6db2670d2be78525979e9a5f9c69d296fd7d670549fe9ebf70f8708cb5019" +dependencies = [ + "bytemuck", + "safe_arch", +] diff --git a/crates/day_6/Cargo.toml b/crates/day_6/Cargo.toml index 4145152..a28815b 100644 --- a/crates/day_6/Cargo.toml +++ b/crates/day_6/Cargo.toml @@ -6,4 +6,6 @@ edition = "2021" [dependencies] solver_base = { workspace = true } -utils = { workspace = true } \ No newline at end of file +utils = { workspace = true } + +nalgebra-glm = "0.19.0" \ No newline at end of file diff --git a/crates/day_6/data/day6_input b/crates/day_6/data/day6_input index e69de29..de833c2 100644 --- a/crates/day_6/data/day6_input +++ b/crates/day_6/data/day6_input @@ -0,0 +1,130 @@ +.#....#...................#................#......................#.#...............#................#....#...........#..........# +.................................#.....#............#............#.......#...#...##........#..............................#....... +..............#.#............#...............................................##.................#....................#..#......... +..............#.............................#...........................#....#....#........#.................................#..#. +............#.#..#.#...........................#.....................................#..........#............#.................... +.#......................................................................#..................................#......#............#.# +...........#.............................#....................#...#.....#........#....##.........................................# +......#..##.......................................................................#............#.................................. +........#.......#..........#.......#........#..................#....................#.............#.....................#......... +................#....#.##.........................#........#....#.................#............................................... +.............#........................................#....................#...............#.....#.............#...............#.. +..........#.........................#...#.......................#.............................................#......#............ +........#................................................#..........#.............#.............#.........#....................#.. +....#............#................#.....#.#................#..................#.................#................................. +....#.........................#.............#.....#..................#....#............#...................................#...... +...##....................#.............#...............................................#.......................................... +.....##.......................#...................#................#..........#.....................#...................#......... +....#....#..................................................#.......................................................#............. +...#........##.........................#.......................#........#...................#...........#...................#..... +.....................##.#...................................................................#.............#....................... +........#..............#...................................................#.....................#...#.......#..#.............#... +..................................................#.......#...............#.............#........#..#.....................#....... +..#..................#...............##..................#.#.........#..........#..............#....................#..#.......... +...#..................................................#...........................................#........#...................... +#.................................................#..............#...#..........#....................#....................#......# +..#...#....#...........#.................................#......................................#.......................#.....#... +...#...........#..#................................#......#.............#........................##.................#....#....#... +....................#.........#.......................................................................................#........... +............................................................#................#.#..#.....#.....................#................... +.......................................#......#....................................#........................................#..... +...........#.............#...............................#..........#.........#...#.......................................#....... +.............#...............................#.#...................................................#.....#............#........... +.......................................#...#....................#.#..........##.................#...............#............#.... +....................#..#....#.............#..............#....#.........................................#.##.....#................ +......##..............................................................................................#........................... +....#.......#.............................#..#.........#................................#.........#............................... +..........................................................................#...............................#...........#.#......... +...............#..................................................#.........#.....................#...............#............... +.........#......#............#.........#................................#..............................##......................... +...............#.....................................#............................................#........................#...... +..#...........#...........................................#.........................................................#............. +................................#......##..#............................................................................#......... +.........#...........................................#....#..............#......................##....#..#........................ +.....#.............#.....#........##......................#...#..................................................#.........#..#.#. +...........................#.........#.......#..........................##...........................#..............#............. +...............................................................................#..#........................#................#.#... +.....#.#.............................................................#.........#...........#..................................#... +.....#....#..#........#....................................#...................................................................... +#......................................#..#..............................#..#................#........#........................... +.....................................................................................................#.......#....#.........#..... +....#....#............................................#................................................................#.......... +......#.................#.......#..............................................#...................#.............................. +...................#.........................................................................................#.................... +.............................#................#.#......#...#.......................................#............#............#.... +...........................................................#..........#..#.................##.....#..#............................ +.....#......................................................#..............................#........................#............. +...............................................#.............#........#.......................................#.......#........... +.............................................................#.............................#...........#.#...................#.... +.......#......#......#......................#..................................#^.......................#.........#.......#....... +...........................................................................................................#....................#. +.....#............#.....................#....................#.................................................................... +....#...........#................#.....##.............................................................#....................#...... +.................................#.#....................#......................#......................#.#.................#....... +..................#......#...#..........#............#................#...#.................................................#..... +...............................................................................................................#...#.........#.... +...............#.........#..............................#.......#.......#.......#................................................. +.#........#...#......................................#........#...#.......................................................#..#.... +.....#...............................................................#.....#..........#.........#.....................#........... +.........................................................................................................#...#.........#......#... +.....##..................#...#.....#.........#......................................#...............................#............. +..#...............................................................................................#............................... +........#..............................#.#........#.#..#...................................................#.#.................... +........................................................#........................#..#............................................. +.....................#.......................#..................................#..........................#....#................. +.........................##.........#.................................#................................#....#..................... +..#........#.......................................................................#.....#........................................ +.....#.....................................#.....#....................#...............#........................................... +......................#....................................................................................................#...... +...#.........................#......................................................#.......#.......#..#.....................#.... +............#...#....................................................#....#......#...............#.......#......................## +.......#.................................................#...#.....................#..................#........................... +.#...................#.....#.#..............................................................................#..................... +..........#...............#......#...........#......................................................#.....#.............#...#....# +.......##.........#.....................................................#....#.........................................#.........# +..##..................#............#..#........................................#..................#..##........................... +..................#....##..............................#.....#...............#.................................................#.. +........#..........#...................#.............#...............#..#........#................................................ +........#..............................................##....#.................................................................... +.........#.#............#.........................#..............#................................#......................#........ +........#.#............................#.....#....................................................................#............... +.......................................#......................#...........................#....................................... +..........................##.....................##..#............................................................................ +#.............#..............................................................................#...........#........................ +......#....#.....................................#........................#....................................................... +#...................#.#.......#.......#.....................#.....#...........................................#................... +...#.......................................#............................................#................#........................ +...........#.............................#........................................................................................ +...............................................................#.............#..................................................#. +.........#........#....#.#..............................................#......................................................... +.........#....#....#...................#..........#............................................#..##.............................. +....#...#.......................#..#....................#...#..........................##..#....#.........................#....... +....#.....##...........................#.#..............#.......................#...#.......................................#..... +......................................................................................#.#..#.....#............##....#....#........ +..............#..................................................................##....#.............#............................ +................##...#..............#........................#.......................#.#...#......................#............... +...#............................#...#......#.#............#........#......................#........#................#............. +..........#.....................................#.....#...............................................................#.........## +.........#.......#...................#.#.........................................................#................................ +...........................#.#............................#...................#.................#.........#....................... +.....#....#.........#................................................................................#...#..................#..... +...............................#....#............................#............................................#.............#..... +......#...............#......#.........................#.#........................................................................ +.......................................................................................#...................#.........#.#..#....... +.....#...................................#..................................................................#........#............ +...........................................#.......................................#....................#............#...#........ +....#............................#..................................#.#....#......................#....#..#.......#...#........... +.#....#................................................#..................#.......##..........................#...#............... +........#...................##...........................................................#...................................#.#.. +...#..........#.#....#...................#......#................................................................#................ +....#............................#....#......#..#............#..................#........#..........#....#....#........#.#........ +................#....#...............#...#.#..............................................#............#................#......... +.......#.....#.#.................#......................................................................#..#...................... +.#......................#.................................................#......#..........#......#....................#......... +......#.#............................#............#.......#............#.....#.................................................... +.#.............#.........#..................................................#...#...........#..........#............#..#.......... +...........#......#...........#............................#.......#......#......#..........#....##..#............#............... +..........................#........#.........................................................#......#........................##... +.................#....#............#...#...........................#....#....#....#...#........................................... +.............#.......................................................#.........#.................................................. +....#.................................#...............#.....#....#......##..............#.......................#........#........ diff --git a/crates/day_6/data/day6_test_input b/crates/day_6/data/day6_test_input index e69de29..b60e466 100644 --- a/crates/day_6/data/day6_test_input +++ b/crates/day_6/data/day6_test_input @@ -0,0 +1,10 @@ +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#..^..... +........#. +#......... +......#... \ No newline at end of file diff --git a/crates/day_6/src/day_6.rs b/crates/day_6/src/day_6.rs index c17a1cb..ee6799f 100644 --- a/crates/day_6/src/day_6.rs +++ b/crates/day_6/src/day_6.rs @@ -11,11 +11,16 @@ use ::solver_base::solver_base::{Solver, DataSet, RunMode}; use utils::utils; +use crate::map::Map; + pub struct Day6 { data_set: DataSet, run_mode: RunMode, do_debug_prints: bool, + + map: Map, + guard_history: Vec>, pub final_result: i32, } @@ -26,12 +31,39 @@ impl Day6 { pub fn new() -> Day6 { - Day6 { data_set: DataSet::Test, run_mode: RunMode::FirstCase, do_debug_prints: false, final_result: 0 } + Day6 { data_set: DataSet::Test, run_mode: RunMode::FirstCase, do_debug_prints: false, + map: Map::new(), guard_history: vec![], final_result: 0 } } fn solve_first_case(self: &mut Self) -> String { - + while self.map.guard_is_on_map() + { + + self.guard_history[self.map.guard_position().x as usize][self.map.guard_position().y as usize] = true; + self.map.step_guard(); + } + + if self.do_debug_prints + { + println!("DEBUG: Guard path prediction complete:"); + self.map.print(); + } + + let mut num_locations = 0; + + for row in &self.guard_history + { + for b in row + { + if *b + { + num_locations += 1; + } + } + } + + self.final_result = num_locations; self.final_result.to_string() } @@ -68,10 +100,16 @@ impl Solver for Day6 DataSet::Full => format!("{}/data/day6_input", dir), }; - let _data = utils::load_data(&data_filename); + let data = utils::load_data(&data_filename); - // TODO: Day6::init + self.map = Map::parse(&data); + + if self.do_debug_prints + { + self.map.print(); + } + self.guard_history = vec![vec![false; self.map.width() as usize]; self.map.height() as usize]; } fn solve(self: &mut Self) -> String diff --git a/crates/day_6/src/map.rs b/crates/day_6/src/map.rs index e69de29..22b8cf4 100644 --- a/crates/day_6/src/map.rs +++ b/crates/day_6/src/map.rs @@ -0,0 +1,205 @@ +/****************************************************************************** +* @file map.rs +* @author Joey Pollack +* @date 2024/12/09 (y/m/d) +* @modified 2024/12/09 (y/m/d) +* @copyright Joseph R Pollack +* @brief Represents the map and the guard on the map. +******************************************************************************/ + +use nalgebra_glm::I32Vec2; + +#[derive(Copy, Clone, PartialEq, Debug)] +pub enum MapCell +{ + Space, + Block, + Guard, + Visited, +} + +#[derive(Copy, Clone, PartialEq, Debug)] +pub enum Direction +{ + North, + South, + East, + West, +} + +#[derive(Clone, Debug)] +pub struct Map +{ + map: Vec>, + guard_direction: Direction, + guard_pos: nalgebra_glm::I32Vec2, +} + +impl Map +{ + pub fn new() -> Map + { + Map { map: vec![], guard_direction: Direction::North, guard_pos: I32Vec2::new(0, 0) } + } + + pub fn parse(data: &str) -> Map + { + let mut split_pattern = "\n"; + if data.contains("\r\n") + { + split_pattern = "\r\n"; + } + + let rows = data.split(split_pattern); + let height = rows.clone().count(); + + let mut map: Vec> = vec![vec![]; height]; + let mut guard_pos = I32Vec2::new(0, 0); + + for (i, row) in rows.enumerate() + { + map[i] = vec![MapCell::Space; row.len()]; + for (j, c) in row.as_bytes().into_iter().enumerate() + { + match c + { + b'.' => map[i][j] = MapCell::Space, + b'#' => map[i][j] = MapCell::Block, + b'^' => { map[i][j] = MapCell::Guard; guard_pos.x = i as i32; guard_pos.y = j as i32; } + + _ => panic!("Map ERROR: Failed to parse map data, Invalid map input: {}", c) + } + } + } + + Map { map, guard_direction: Direction::North, guard_pos } + } + + pub fn print(self: &Map) + { + print!("MAP:"); + for (i, row) in self.map.iter().enumerate() + { + print!("\n\t"); + for (j, c) in row.iter().enumerate() + { + + if i as i32 == self.guard_pos.x && j as i32 == self.guard_pos.y + { + self.print_guard(); + } + else + { + match *c + { + MapCell::Space => print!("."), + MapCell::Block => print!("#"), + MapCell::Visited => print!("X"), + MapCell::Guard => self.print_guard(), + } + } + + } + } + println!(); + } + + fn print_guard(self: &Map) + { + match self.guard_direction + { + Direction::North => print!("^"), + Direction::South => print!("v"), + Direction::East => print!(">"), + Direction::West => print!("<"), + } + } + + pub fn width(self: &Map) -> i32 + { + self.map[0].len() as i32 + } + + pub fn height(self: &Map) -> i32 + { + self.map.len() as i32 + } + + /// Step Rules: + /// + /// If there is something directly in front of you, turn right 90 degrees. + /// Otherwise, take a step forward. + /// + /// Returns true if the guard is still on the map after the step. + pub fn step_guard(self: &mut Map) -> bool + { + if !self.guard_is_on_map() + { + return false; + } + + let next_guard_pos = self.guard_pos + self.guard_direction_vector(); + + if !self.pos_is_on_map(&next_guard_pos) + { + self.map[self.guard_pos.x as usize][self.guard_pos.y as usize] = MapCell::Visited; + self.guard_pos = next_guard_pos; + return false; + } + + if self.map[next_guard_pos.x as usize][next_guard_pos.y as usize] == MapCell::Block + { + self.rotate_guard_90(); + return true; + } + + self.map[self.guard_pos.x as usize][self.guard_pos.y as usize] = MapCell::Visited; + self.guard_pos = next_guard_pos; + self.guard_is_on_map() + } + + pub fn guard_is_on_map(self: &Map) -> bool + { + self.guard_pos.x >= 0 && + self.guard_pos.y >= 0 && + (self.guard_pos.x as usize) < self.map[0].len() && + (self.guard_pos.y as usize) < self.map.len() + } + + fn pos_is_on_map(self: &Map, pos: &I32Vec2) -> bool + { + pos.x >= 0 && + pos.y >= 0 && + (pos.x as usize) < self.map[0].len() && + (pos.y as usize) < self.map.len() + } + + pub fn guard_position(self: &Map) -> I32Vec2 + { + self.guard_pos + } + + pub fn guard_direction_vector(self: &Map) -> I32Vec2 + { + match self.guard_direction + { + Direction::North => I32Vec2::new(-1,0), + Direction::South => I32Vec2::new(1, 0), + Direction::East => I32Vec2::new(0,1), + Direction::West => I32Vec2::new(0, -1), + } + } + + // Rotate the guard clockwise 90 degrees + fn rotate_guard_90(self: &mut Map) + { + match self.guard_direction + { + Direction::North => self.guard_direction = Direction::East, + Direction::South => self.guard_direction = Direction::West, + Direction::East => self.guard_direction = Direction::South, + Direction::West => self.guard_direction = Direction::North, + }; + } + +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index a3b1b53..04a0cf9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,7 @@ use day_5::day_5::Day5; use day_6::day_6::Day6; use solver_base::solver_base::{Solver, DataSet, RunMode}; -fn main() +fn main() { // DAY 1 let mut day_1 = Day1::new(); @@ -53,7 +53,7 @@ fn main() let mut day_3 = Day3::new(); day_3.init(DataSet::Full, RunMode::SecondCase, false); let day3_result = day_3.solve(); - println!("Day 3 Part 2 Final Result: {}", day3_result); + println!("Day 3 Part 2 Final Result: {}", day3_result); println!("-------------------------"); @@ -66,8 +66,8 @@ fn main() let mut day_4 = Day4::new(); day_4.init(DataSet::Full, RunMode::SecondCase, false); let day4_result = day_4.solve(); - println!("Day 4 Part 2 Final Result: {}", day4_result); - + println!("Day 4 Part 2 Final Result: {}", day4_result); + println!("-------------------------"); // DAY 5 @@ -79,19 +79,21 @@ fn main() let mut day_5 = Day5::new(); day_5.init(DataSet::Full, RunMode::SecondCase, false); let day5_result = day_5.solve(); - println!("Day 5 Part 2 Final Result: {}", day5_result); + println!("Day 5 Part 2 Final Result: {}", day5_result); + println!("-------------------------"); + // DAY 6 let mut day_6 = Day6::new(); day_6.init(DataSet::Full, RunMode::FirstCase, false); let day6_result = day_6.solve(); println!("Day 6 Part 1 Final Result: {}", day6_result); - let mut day_6 = Day6::new(); - day_6.init(DataSet::Full, RunMode::SecondCase, false); - let day6_result = day_6.solve(); - println!("Day 6 Part 2 Final Result: {}", day6_result); - + // let mut day_6 = Day6::new(); + // day_6.init(DataSet::Full, RunMode::SecondCase, false); + // let day6_result = day_6.solve(); + // println!("Day 6 Part 2 Final Result: {}", day6_result); + println!("-------------------------"); }