From 4f90390833dc8bc8930b9f42588459fce3dddf3d Mon Sep 17 00:00:00 2001 From: itsscb Date: Wed, 18 Dec 2024 00:16:50 +0100 Subject: [PATCH] day06: incomplete --- src/day05/mod.rs | 12 +--- src/day06/guard.rs | 86 +++++++++++++++++++++++++++++ src/day06/input.txt | 130 ++++++++++++++++++++++++++++++++++++++++++++ src/day06/mod.rs | 109 +++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + src/main.rs | 30 ++++++---- 6 files changed, 347 insertions(+), 21 deletions(-) create mode 100644 src/day06/guard.rs create mode 100644 src/day06/input.txt create mode 100644 src/day06/mod.rs diff --git a/src/day05/mod.rs b/src/day05/mod.rs index d021bbc..b0ce335 100644 --- a/src/day05/mod.rs +++ b/src/day05/mod.rs @@ -1,6 +1,6 @@ use std::{collections::HashMap, error::Error, fs}; -/// Solves the problem for day 04. +/// Solves the problem for day 05. /// /// # Errors /// @@ -69,12 +69,6 @@ fn order_updates(updates: &[i32], rules: &HashMap>) -> Vec { for (key, values) in rules { if a == key && values.contains(b) { return std::cmp::Ordering::Less; - // } else if b == key && values.contains(a) { - // return std::cmp::Ordering::Greater; - // } else if a < b { - // return std::cmp::Ordering::Less; - // } else if a > b { - // return std::cmp::Ordering::Greater; } } std::cmp::Ordering::Equal @@ -142,22 +136,18 @@ mod tests { let want = vec![75, 29, 13]; let got = order_updates(&updates[2], &rules); - dbg!(&got, &updates[2]); assert_eq!(got, want); let want = vec![97, 75, 47, 61, 53]; let got = order_updates(&updates[3], &rules); - dbg!(&got, &updates[3]); assert_eq!(got, want); let want = vec![61, 29, 13]; let got = order_updates(&updates[4], &rules); - dbg!(&got, &updates[4]); assert_eq!(got, want); let want = vec![97, 75, 47, 29, 13]; let got = order_updates(&updates[5], &rules); - dbg!(&got, &want, &updates[5]); assert_eq!(got, want); let want = 61; diff --git a/src/day06/guard.rs b/src/day06/guard.rs new file mode 100644 index 0000000..05133bf --- /dev/null +++ b/src/day06/guard.rs @@ -0,0 +1,86 @@ +use std::collections::HashSet; + +#[derive(Debug, PartialEq, Eq, Clone)] +pub struct Guard { + x: usize, + y: usize, + direction: Direction, + visited: HashSet<(usize, usize)>, +} + +impl Guard { + #[allow(dead_code)] + pub fn new(x: usize, y: usize, direction: Direction) -> Self { + let mut visited = HashSet::new(); + visited.insert((x, y)); + Self { + x, + y, + direction, + visited, + } + } + + #[allow(dead_code)] + pub const fn direction(&self) -> &Direction { + &self.direction + } + + #[allow(dead_code)] + pub const fn start(&self) -> (usize, usize) { + (self.x, self.y) + } + + #[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)] + pub fn visited(&self) -> i32 { + self.visited.len() as i32 + } + + #[allow(dead_code)] + pub const fn next_move(&self) -> (usize, usize) { + match self.direction { + Direction::Up => (self.x - 1, self.y), + Direction::Down => (self.x + 1, self.y), + Direction::Left => (self.x, self.y - 1), + Direction::Right => (self.x, self.y + 1), + Direction::Unknown => (self.x, self.y), + } + } + + #[allow(dead_code)] + pub fn move_guard(&mut self, direction: &Direction) { + match direction { + Direction::Up => self.x -= 1, + Direction::Down => self.x += 1, + Direction::Left => self.y -= 1, + Direction::Right => self.y += 1, + Direction::Unknown => { + return; + } + } + self.direction = direction.clone(); + self.visited.insert((self.x, self.y)); + println!("Moved to: x: {} | y: {}", self.x, self.y); + } +} + +#[derive(Debug, PartialEq, Eq, Clone)] +pub enum Direction { + Up, + Down, + Left, + Right, + Unknown, +} + +impl From for Direction { + fn from(c: char) -> Self { + match c { + '^' => Self::Up, + 'v' => Self::Down, + '<' => Self::Left, + '>' => Self::Right, + _ => Self::Unknown, + } + } +} diff --git a/src/day06/input.txt b/src/day06/input.txt new file mode 100644 index 0000000..b10d972 --- /dev/null +++ b/src/day06/input.txt @@ -0,0 +1,130 @@ +................#......#...........#...........................#......#...........#........................................#...... +.......................................................................................#........#............#.............#.....# +#.......................#...#..........#...........#........#.......................#............#................................ +.............#...............................................#........#................................#....#......#.............. +...#..................................#......................#....#....................................#.................#........ +...................#.....#......#...........#............................#......................#.#........................#...... +...........................#..#...........#.............................#.........#.##.............#.............................. +.#.................#................#..........##.........##..............#...................................................#... +...............................#......................#........#.......................#.........#...........#.................... +...................................#................#............................................................................. +..#...............##.......#................#.#......#...........#................................................................ +....#.#.....#..............#........##........................##...........................................#...............#...... +.....................#..........#.................................#.....................................#......................... +..............#.........................................##......#.......................#..............#.................#..#..... +...........#...#..........................................................#.......#.............................#................# +................................#.#...............#...............................#..............##............................... +.........................................................#............#......................................#.................#.. +......#.#....#.................................#....###..................#......#.........#....#.................................. +..............#...................................#..........#............#.......#............................................... +..............#......#...............#.............................................#.............................................# +....#.#..............#................#......................................................................#.................... +........................................................#...........#..#.#.....................#....#.....#........#...#.......#.. +..............#.......#................#............................#..#.......#.........#...........#...#...#....#....#.....#.... +...#...............................................................................................#..#..#..........#............. +.......#.........#..###..................................##...............##..#.......................#..........#...........#...# +.......#.....................#.............#...........#...............................................#.................#........ +..........#............................#..............................#......#..#.........#...............................#....... +.............................#...............#............#..............................................................#........ +...........#...................#......#......................#.......................#...............#......#..................... +..#..............................................#.........................#.............#...#.#.................................. +................#.............................#....................................#.....#...................#..#.....#........... +#............................#...#........................#................#....#.............................................#... +...............#.............#........#...................................................................#.......#....#..#....... +...........#......#.....#........................#.#..........................................#............#...................... +.#.....................................................#...............#.........................#................#...........#... +.........................................#.............#......#.#....................................................#.........#.. +................................#...#......................................................#..........................#..........# +.#.........................#..............#.#..................................................................................... +.........#..........#.............##.........#..#.................#........................#.....#.##....................#........ +...................................................................................#...........................................#.. +................#..........................................#...#...#.................................................#.........#.. +..............................................#..............#..................#......#...#....#..#.............................. +......#......#.................#.............#..................#...............#.............#..................#...............# +.................................#.......#......#.......#.............................................................#........#.. +..................#......#...................................#.................#.......#.......................................... +....................#.........#.........................#..........#..........#..................#................................ +..........#...........#...............#.#..................................................................................#...... +..................#..#....................................................#..........#......#................................#.... +............#.................................................................................................#........#.......... +.........#...............................................#.........##...........................#................................. +...#.........#........................................................#....................................#...................... +........#......................#......#............#........#...............#.......................#............................. +.......#..............................................................................................#....................#...... +.#.#..........#.......#.............##......................#..........................................................#.......... +............#.#...........#............................................................#.......................................... +............#..........................................................................#....................#...............#..... +.......................#.....#...#........................................#.......#.........#.........................#........... +....................#..............#......#....................................................................................... +......................#....................................................................#...............#...#............#..... +........#..........#....................#...#........#......#..........................#....#..................#..#............... +................#...........................................................................................................#..... +.#...............#..........................................................................................#......#..#.#......... +..............#................#...............#.........#.........#................................#.#........................... +...#................#..............#........................#...#.......................................#.#....................... +#.........................................#..#....................................................................#............... +.............#...........#.......#.#.....................................................................#.....#.................. +.............#..............................................................................#..................................... +........................#........#....#........#.#.............................................^..........#....................... +...............................#.......................................................#.............##.......#.......#........... +#.#..............#..........................................#.......#..........................................#.................. +...##...............................#........#.............##..................................................#.................. +..#..#.................................................................#............#...#...#..................................... +....................#.#...#............#.......#.............#.......#..............#............................................. +..#..#.........#.............................................................................#......................#...#......... +....................................................#.........................#..#...#.#.#......#...............................#. +......#.....#.....................................................................#..............................................# +....................#........#..........................................................#.......#........................#........ +................#.........................#...........................#...........................................#..............# +..#.......#..................................................#..................................#................................. +..........#.....................#...#.#...........#....................................#.......................................... +.....................................#...........................................................#..........#...................#. +....#............................#.........................................#.........................#............................ +......#............#...........#......#.#..................................#...................................................... +..................#.......#.....#......#.................................................................#................#....... +...........#.....#........................................................#.............#......................................... +.................................#...........................#........#.........#............#.............................#.....# +..#........................#.................................#.................................................................... +......................................................................................................#........#.#...#............ +#............................#..........#.#.....................#.............................................................#... +.......#.......#................#..........#..........................#........#....#...#........................................# +....#.....#..........................................................#.........................#....................#..#.......#.. +.......#.............#.................................#...................................................................#...... +...............#.......................................#.#...............................#.#..........#........................... +....#......#...#.#.........................#...............#...........#.......#........................................#..#...... +...................#.........................................#.....................................................#..#.....#...#. +....#......#.#.............................#..........##........#......................#.#.......................#....#........... +........#.........................#........#.#..........................##..............#....#.................................... +.............#.....#..........#.....#.......#..##...#.....................#..................................#.................... +....#....#...........................#...........#....................#.....#.......#....#.........#.....#....#................... +............#...#....................#..........#.......#......#............#.........................#............##............. +.....................#.................................................................####....................................... +...#...........................................................................#......#..............#....#.......#............... +....#...#...#.....................#.............#..............................#...................#...........#.................. +............................#...........#........#.............#.................................................................. +...#.....#......#.....................#...........................................#...........#........#.......................... +.................#...........................#................................................................#..........#........ +.....................#........#...#...................................................................................#......#.... +.#...............................#...#....#.................#...#......#.......................................................... +....................#..........#.........................................#....................#.........#......................... +.......#.............#............#................#..........#.#.#.....................#............#.....................#...... +......................#.........................#.................#...........................#................#.................. +...#....#..........................#.#..##.................#..#..........#..............................................#......... +...........#.........................................................................................#.............#.............. +..............#.................##...........#..#.............................................#..............................#.... +...............................................##.......#..#.....#........#...#.............#.....................#............... +........#...................#...........#...............................#............................................#........#... +...................................#.....#......................#...#.........##..#...................#.#......................... +............#...........##............#.......................#.....#.......#...................#................................. +..#..#..............................#.......#.........#.....................................#.............#..#.................#.. +..................#..................#..........................##..................#...#.......#.........#..................#.... +.........#.........#..............................................#...........................................................#... +......................#...........#..................................#............................#......#.....#.................. +.....#....#......#..................#.....#..............#.............................................#.....#.................... +..............##...........#........................................................................#........#.#.................. +..........#...............................##.......#............#.#.........#.....................................#....#.......... +.................................#..............#.............................#........................................#.......... +....................#.................#..........#..............#.......................#...................................#..... +............#..............................#.............................#......#......#......................#...#............... +.#..........#...............#..................................................................#....#............................. +....#............................................#......#..................#.....#............#.........................#......... \ No newline at end of file diff --git a/src/day06/mod.rs b/src/day06/mod.rs new file mode 100644 index 0000000..e792e79 --- /dev/null +++ b/src/day06/mod.rs @@ -0,0 +1,109 @@ +use std::{error::Error, fs}; + +use guard::Guard; + +mod guard; + +/// Solves the problem for day 06. +/// +/// # Errors +/// +/// This function will return an error if the file cannot be read or if the input is invalid. +#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)] +pub fn solve_day06(path: &str) -> Result<(i32, i32), Box> { + let content = fs::read_to_string(path)?; + + let (mut guard, map) = parse_input(&content); + let part_two = find_possible_obstacles(guard.start(), &map); + calc_guard_path(&mut guard, &map); + let part_one = guard.visited(); + + Ok((part_one, part_two)) +} + +#[allow( + unused_mut, + clippy::cast_possible_truncation, + clippy::cast_possible_wrap +)] +fn find_possible_obstacles(_start: (usize, usize), _map: &[Vec]) -> i32 { + let mut obstacles: Vec<(usize, usize)> = vec![]; + + obstacles.len() as i32 +} + +fn calc_guard_path(guard: &mut Guard, map: &[Vec]) { + loop { + let (x, y) = guard.next_move(); + if x >= map.len() || y >= map[0].len() { + break; + } + // dbg!(x, y); + let c = map[x][y]; + if c == '#' { + let new_direction = match guard.direction() { + guard::Direction::Up => &guard::Direction::Right, + guard::Direction::Down => &guard::Direction::Left, + guard::Direction::Left => &guard::Direction::Up, + guard::Direction::Right => &guard::Direction::Down, + guard::Direction::Unknown => &guard::Direction::Unknown, + }; + guard.move_guard(new_direction); + } else { + let direction = guard.direction().clone(); + guard.move_guard(&direction); + } + } +} + +fn parse_input(input: &str) -> (guard::Guard, Vec>) { + let mut guard = guard::Guard::new(0, 0, guard::Direction::Unknown); + let map = input + .split('\n') + .enumerate() + .flat_map(|(i, r)| { + if r.contains('^') || r.contains('v') || r.contains('<') || r.contains('>') { + let () = r + .chars() + .enumerate() + .filter(|(_, c)| *c == '^' || *c == 'v' || *c == '<' || *c == '>') + .map(|(j, c)| { + let d = guard::Direction::from(c); + guard = guard::Guard::new(i, j, d); + }) + .collect::<()>(); + } + vec![r.chars().collect()] + }) + .collect(); + (guard, map) +} + +#[cfg(test)] +mod tests { + use guard::Guard; + + use super::*; + + #[test] + fn test_solve_day06() { + let input = "....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#........ +........#. +#......... +......#^.."; + + let (mut guard, map) = parse_input(input); + assert_eq!(guard, Guard::new(9, 7, guard::Direction::Up)); + + calc_guard_path(&mut guard, &map); + let want = 7; + let got = guard.visited(); + assert_eq!(got, want); + } +} diff --git a/src/lib.rs b/src/lib.rs index f290640..d945a75 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,3 +3,4 @@ pub mod day02; pub mod day03; pub mod day04; pub mod day05; +pub mod day06; diff --git a/src/main.rs b/src/main.rs index 1e6549a..1aa4753 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use adventofcode_24::{ day01::solve_day01, day02::solve_day02, day03::solve_day03, day04::solve_day04, - day05::solve_day05, + day05::solve_day05, day06::solve_day06, }; use clap::{Arg, Command}; @@ -34,11 +34,16 @@ fn main() { .short('5') .help("Path to Day 05 Input file"), ) + .arg( + Arg::new("day06") + .short('6') + .help("Path to Day 06 Input file"), + ) .get_matches(); if let Some(file) = matches.get_one::("day01") { match solve_day01(file) { - Ok((d, s)) => println!("Result of Day 01:\nDistance: {d}\nScore: {s}"), + Ok((d, s)) => println!("Result of Day 01:\nPart one: {d}\nPart two: {s}"), Err(e) => eprintln!("{e}"), } } @@ -46,9 +51,7 @@ fn main() { if let Some(file) = matches.get_one::("day02") { match solve_day02(file) { Ok((r1, r2)) => { - println!( - "Result of Day 02:\nSafe Reports: {r1}\nSafe Reports with tolerance: {r2}" - ); + println!("Result of Day 02:\nPart one: {r1}\nPart two: {r2}"); } Err(e) => eprintln!("{e}"), } @@ -57,9 +60,7 @@ fn main() { if let Some(file) = matches.get_one::("day03") { match solve_day03(file) { Ok((r1, r2)) => { - println!( - "Result of Day 03:\nSum of uncorrupted memory multiplications: {r1}\nSum of uncorrupted and enabled memory multiplications: {r2}" - ); + println!("Result of Day 03:\nPart one: {r1}\nPart two: {r2}"); } Err(e) => eprintln!("{e}"), } @@ -68,7 +69,7 @@ fn main() { if let Some(file) = matches.get_one::("day04") { match solve_day04(file) { Ok((r1, r2)) => { - println!("Result of Day 04:\nXMAS count: {r1}\nX-MAS count: {r2}"); + println!("Result of Day 04:\nPart one: {r1}\nPart two: {r2}"); } Err(e) => eprintln!("{e}"), } @@ -77,7 +78,16 @@ fn main() { if let Some(file) = matches.get_one::("day05") { match solve_day05(file) { Ok((r1, r2)) => { - println!("Result of Day 05:\nOrdered middle page count: {r1}\nPart two: {r2}"); + println!("Result of Day 05:\nPart one: {r1}\nPart two: {r2}"); + } + Err(e) => eprintln!("{e}"), + } + } + + if let Some(file) = matches.get_one::("day06") { + match solve_day06(file) { + Ok((r1, r2)) => { + println!("Result of Day 05:\nPart one: {r1}\nPart two: {r2}"); } Err(e) => eprintln!("{e}"), }