diff --git a/inputs/day_6.txt b/inputs/day_6.txt new file mode 100644 index 0000000..fed9be4 --- /dev/null +++ b/inputs/day_6.txt @@ -0,0 +1,130 @@ +.......................#....#....................#....#..##.##.........................................#...........#...........#.. +.............#........................................#....................#............#.......................#............#.... +.........#.............#..#....#..........................#.......#.#.......#...............................................##.... +..........#....#....................................................#....................#...........#....#....................... +.............#...#....................#...........#.........#...........................#............#............................ +..........................................................................................#.....#.......#...................##.... +...............................................#.#......#..............#...#...................##...........#....#..#...#......... +..#.................#......#........#......................#.......#...............................#.............................. +.........................#...............#.....................................#....#..#..#............................#.......... +......................#.................................................................#..........#..............#...#......#.... +............................#.....##..........#.....................#..........#..#.......#....................................... +......#...###...........#.................................................................#....................................... +......................#.#..#................#..........#................#...........#............#..............#...#.........#... +....#.........................#..#...............................................#....#.............#............................. +..#............#...#............#..........#...............#.....#....##.........................#........#....................... +..................#............#..............#..........................................................#..#..#........#......... +.......#...#............#..........................................................#..............................#.............#. +..#................#....................#.#...#.................................#.........#....................#.........#........ +...................................#..#..........................#........#.....#.........................................#....... +...........##.................##............#................#...#..................................##...#........................ +..............#..................#....................................................................#........................... +.#...#....#..........##..................................#.............................................................#......#... +......#.....#................................#...............................................................#......#............. +..........#.............................................#............................##....#.........................#............ +..........#......#.................#...#........#...........#.#..........#........#....................................#.......... +.#..................#.#............................#.......................#.........#.......#.................................... +.....................#....#..................#..................................................##..#................#............ +......#......................#..#..............##.......#..................................................#..............#....... +..........................................................................#........#...#..............#.................#......... +....................##....#.......................#........#...................#.............##.......#........................... +.........#............#..................................................................................#........................ +........................................................................#................#........................#.............#. +............#.......#.................................#............#.............................................................. +.#.............#...............................................#....#.........#....#..................................#.#......... +.......................................#.....................................................................................#.... +........#........................................#...............#........................................................#....... +...............................#.........#........#...........#.......................................................#.........#. +.....................##...#....................................................#............#..................#..#..........##... +................#..........#...#............................................................#...##................................ +...................#........#...............................#.................#.........#......................................... +.................................................................................................................#................ +............................#......................................................#.....................................#......#. +......#................#............................#............................................................................. +..#...........#.....#.........#....#................#............................................#................................ +..................................#.......#.......#...........#.........#................#.....................................#.# +...............................................#............................................#.#.....................#.......##.... +..............................#...............#..............................#...#...#.............................#...........#.. +.......#...........#..........................................................#........#..#.........#.....#.....#...#............. +......................................................#.#............#.........#.#...........#........##.....#.................... +...........................................................................#..........#.........................#.........#....... +....#.........................#.....................................................................................#...#......#.. +...#.......#............................................................#...............................#....................##.#. +.................................#.....#................................................................#......................... +.....................................#........................#....................................#.............................. +..............#.......................................................................#.......#....................#..........#... +................................#..........................................#.................................#.....#.............. +...........#.......................................................#...............#.....................#..............#......#.. +..................................#..........................#.................................................................#.. +.............##......#....................................................#......#...............................................# +#.......#...................#..........#...................#.................#..................................................#. +.......#..........#.....##...........#.......................#............#..............................#.............#.......... +.............#...................##......................................................................................#........ +#.............................#...................................................................#............................... +.#...................................#....##..................................................................................#... +.................................#.#..................#..#..#..........................#.......#.................................. +..................#....#....#....................................................#....................................#........#.. +..........................................#...#.....#...........#.........#............#............#..........................#.. +...............................................................................#..............................................#... +.............................#......#....................#....#....................................................#.#.#.......... +................##......#........#........#...........#..........................#......................#..............#.......... +.....................#.....#..#.............................................................................................#..... +.......#..#...........#................................................................#.......................................... +#.......................................................................................................#...#..................... +.#..........................................................#.....................................................#............... +#.....#...#...............#..#......#........#..#...................#...................#.................#....................... +..#.................................................................................#.#......................#.................... +.......................#.............#..........#.......................#...................................#..................... +.................#......#......#...#.#.#.............................#.......................................................#.... +..#...........................................................................................................#................... +.................................#..................#.....#........#.........................................................#.... +.............#......................#...............................................#...#.............................#...#....#.. +.#............................#..........................#.#...........................#..........................#.#............. +.............................#.........#.................##............................#...................#...................... +.........#......#....#.........#..............................................................................................#..# +.....................#......#.................#..#..........................#..................#..............................#.#. +........................................................#......................#............#.......#......#...................... +........................................#....##.#........#......#..............................#..........#..........#.##......... +.............#.......#.......#......................#..........................................#.........#............#...#...#.#. +..................#........................................................#...............................#.....#.....#.......... +.........#.................#........#..............^..............#.................##......#...................................#. +....#...................#.#.................................#...#.................................................#............... +.................#.............................................#................#.....#......#.......#...................#........ +...............#...............................................................................#..#...............#............... +........................................##..........................................................................##............ +............................................#...................#........#...#...#....#.........#...................#............. +.....#...#...................#....................................#......#...........#...............#............................ +........................#..................#.....................#.................................................#......##...... +...#.................#....................#..................................#.#.........................#.......#................ +....................#............#..................................#...#......#....................#...............#............. +.....#...............#.......###........#............#......................#.......#....#........................................ +....................#........#...........#..................................#..#................#.............#................... +.....#..................#...................#.#..................................#.......#..#..................................... +..#..#.......#........................................#.................................................................#......... +.......#............#...#....#.......................#.#...................................#.........................#..#.#....... +..............#......................................................................................................#............ +............#....................#........................#.........#...#......................................................... +...................................#..............................#.........................................................#..... +......##...........#.#........................#.........................................................................#......... +..........................#.....................................................#..............................#..............#... +.........................#...#.....................#..#................#................................................#....#.... +...#.................................#...#.......#...............................................................#................ +..................................#....................................#...........................##..#.#.....................#.. +.#.............#.....#...............................................................#...............#...............#..#......... +.........#.............................#........................#.............#......................#........#............#...... +...................................#..#...#.....................................#................................................. +........................................##..........#..............................................#.....................#........ +......#...........................................#....#.....................#...................#..#.........#................... +.#....#.....#........##..................................#......#................................................#....#..##....... +..........#....................................................................##.........................#...........#........... +...........#.#......#.......#..................#............#...............................................#...#..............#.. +.................................................................#.......#.........#.....#........#....##....#..........#......... +.............................#......................................................#.....#...#...#....#...........#..........#.#. +.....................#........#.........................#......................................#................#...............#. +.........................................................................#...................................................#.... +..............#...#..#................#....................................#.......#....#.#...............#.........#............. +.......#......#.#............................................#.......#............................................................ +##......................................................#..#.................................##...................#..#...........# +...........#.#...#........#............#..............................#........................#.........................#..#..... +................................................##.........................................#.................#.................... +............................#....................................................#...#........................#.#......#........#. diff --git a/src/main.rs b/src/main.rs index 98b590f..0fea0ed 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,7 @@ fn main() -> std::io::Result<()> { Ok(3) => solutions::day_3::run(args)?, Ok(4) => solutions::day_4::run(args)?, Ok(5) => solutions::day_5::run(args)?, + Ok(6) => solutions::day_6::run(args)?, Ok(_) => eprintln!("Unknown day. Are you from the future?"), Err(_) => eprintln!("Unknown day format. Use integers to specify."), } diff --git a/src/solutions/day_6.rs b/src/solutions/day_6.rs new file mode 100644 index 0000000..3d03fe9 --- /dev/null +++ b/src/solutions/day_6.rs @@ -0,0 +1,153 @@ +use std::env; +use std::fs::File; +use std::io::{BufRead, BufReader}; + +#[derive(Copy, Clone, Debug, PartialEq)] +enum Field { + Obstacle, + Unvisited, + Visited, +} + +impl Field { + fn blocked(&self) -> bool { + *self == Field::Obstacle + } +} + +#[derive(Copy, Clone, Debug)] +enum Direction { + Up, + Right, + Down, + Left, +} + +impl Into for char { + fn into(self) -> Direction { + match self { + '^' => Direction::Up, + '>' => Direction::Right, + 'v' => Direction::Down, + '<' => Direction::Left, + _ => panic!("Unknown direction {self}"), + } + } +} + +struct Guard { + pub x: usize, + pub y: usize, + pub dir: Direction, +} + +pub fn run(mut args: env::Args) -> std::io::Result<()> { + let filename = args + .next() + .expect("You must provide a filename as argument"); + + let f = File::open(filename)?; + let reader = BufReader::new(f); + + let mut map: Vec> = Vec::new(); + let mut guard: Guard = Guard { + x: 0, + y: 0, + dir: Direction::Up, + }; + + let mut y = 0; + for line in reader.lines() { + let l = line.unwrap(); + + map.push( + l.chars() + .enumerate() + .map(|(idx, c)| match c { + '.' => Field::Unvisited, + '#' => Field::Obstacle, + '^' | '>' | 'v' | '<' => { + guard.y = y; + guard.x = idx; + guard.dir = c.into(); + Field::Visited + } + _ => panic!("Illegal field formatter: '{c}'"), + }) + .collect(), + ); + + y += 1; + } + + // simulate guard movement + loop { + match guard.dir { + Direction::Up => { + if guard.y == 0 { + break; + } else { + if map[guard.y - 1][guard.x].blocked() { + guard.dir = Direction::Right; + continue; + } else { + guard.y -= 1; + } + } + } + Direction::Right => { + if (guard.x + 1) == map[guard.y].len() { + break; + } else { + if map[guard.y][guard.x + 1].blocked() { + guard.dir = Direction::Down; + continue; + } else { + guard.x += 1; + } + } + } + Direction::Down => { + if (guard.y + 1) == map.len() { + break; + } else { + if map[guard.y + 1][guard.x].blocked() { + guard.dir = Direction::Left; + continue; + } else { + guard.y += 1 + } + } + } + Direction::Left => { + if guard.x == 0 { + break; + } else { + if map[guard.y][guard.x - 1].blocked() { + guard.dir = Direction::Up; + continue; + } else { + guard.x -= 1 + } + } + } + } + + // if we haven't exited yet we must update the field + map[guard.y][guard.x] = Field::Visited; + } + + // Task 1 + let sum_visited = map.iter().fold(0, |acc, v| { + v.iter().fold( + acc, + |a, elem| if *elem == Field::Visited { a + 1 } else { a }, + ) + }); + println!("Sum of visited fields: {sum_visited}"); + + // Task 2 + // TODO + + Ok(()) +} diff --git a/src/solutions/mod.rs b/src/solutions/mod.rs index 6bba404..c85b4ee 100644 --- a/src/solutions/mod.rs +++ b/src/solutions/mod.rs @@ -3,4 +3,5 @@ pub mod day_2; pub mod day_3; pub mod day_4; pub mod day_5; +pub mod day_6;