Add solutions for day 6.1

This commit is contained in:
Felix Suchert 2025-01-13 01:16:58 +01:00
parent 409317a451
commit 6b6d77f99b
Signed by: feliix42
GPG key ID: 24363525EA0E8A99
4 changed files with 285 additions and 0 deletions

130
inputs/day_6.txt Normal file
View file

@ -0,0 +1,130 @@
.......................#....#....................#....#..##.##.........................................#...........#...........#..
.............#........................................#....................#............#.......................#............#....
.........#.............#..#....#..........................#.......#.#.......#...............................................##....
..........#....#....................................................#....................#...........#....#.......................
.............#...#....................#...........#.........#...........................#............#............................
..........................................................................................#.....#.......#...................##....
...............................................#.#......#..............#...#...................##...........#....#..#...#.........
..#.................#......#........#......................#.......#...............................#..............................
.........................#...............#.....................................#....#..#..#............................#..........
......................#.................................................................#..........#..............#...#......#....
............................#.....##..........#.....................#..........#..#.......#.......................................
......#...###...........#.................................................................#.......................................
......................#.#..#................#..........#................#...........#............#..............#...#.........#...
....#.........................#..#...............................................#....#.............#.............................
..#............#...#............#..........#...............#.....#....##.........................#........#.......................
..................#............#..............#..........................................................#..#..#........#.........
.......#...#............#..........................................................#..............................#.............#.
..#................#....................#.#...#.................................#.........#....................#.........#........
...................................#..#..........................#........#.....#.........................................#.......
...........##.................##............#................#...#..................................##...#........................
..............#..................#....................................................................#...........................
.#...#....#..........##..................................#.............................................................#......#...
......#.....#................................#...............................................................#......#.............
..........#.............................................#............................##....#.........................#............
..........#......#.................#...#........#...........#.#..........#........#....................................#..........
.#..................#.#............................#.......................#.........#.......#....................................
.....................#....#..................#..................................................##..#................#............
......#......................#..#..............##.......#..................................................#..............#.......
..........................................................................#........#...#..............#.................#.........
....................##....#.......................#........#...................#.............##.......#...........................
.........#............#..................................................................................#........................
........................................................................#................#........................#.............#.
............#.......#.................................#............#..............................................................
.#.............#...............................................#....#.........#....#..................................#.#.........
.......................................#.....................................................................................#....
........#........................................#...............#........................................................#.......
...............................#.........#........#...........#.......................................................#.........#.
.....................##...#....................................................#............#..................#..#..........##...
................#..........#...#............................................................#...##................................
...................#........#...............................#.................#.........#.........................................
.................................................................................................................#................
............................#......................................................#.....................................#......#.
......#................#............................#.............................................................................
..#...........#.....#.........#....#................#............................................#................................
..................................#.......#.......#...........#.........#................#.....................................#.#
...............................................#............................................#.#.....................#.......##....
..............................#...............#..............................#...#...#.............................#...........#..
.......#...........#..........................................................#........#..#.........#.....#.....#...#.............
......................................................#.#............#.........#.#...........#........##.....#....................
...........................................................................#..........#.........................#.........#.......
....#.........................#.....................................................................................#...#......#..
...#.......#............................................................#...............................#....................##.#.
.................................#.....#................................................................#.........................
.....................................#........................#....................................#..............................
..............#.......................................................................#.......#....................#..........#...
................................#..........................................#.................................#.....#..............
...........#.......................................................#...............#.....................#..............#......#..
..................................#..........................#.................................................................#..
.............##......#....................................................#......#...............................................#
#.......#...................#..........#...................#.................#..................................................#.
.......#..........#.....##...........#.......................#............#..............................#.............#..........
.............#...................##......................................................................................#........
#.............................#...................................................................#...............................
.#...................................#....##..................................................................................#...
.................................#.#..................#..#..#..........................#.......#..................................
..................#....#....#....................................................#....................................#........#..
..........................................#...#.....#...........#.........#............#............#..........................#..
...............................................................................#..............................................#...
.............................#......#....................#....#....................................................#.#.#..........
................##......#........#........#...........#..........................#......................#..............#..........
.....................#.....#..#.............................................................................................#.....
.......#..#...........#................................................................#..........................................
#.......................................................................................................#...#.....................
.#..........................................................#.....................................................#...............
#.....#...#...............#..#......#........#..#...................#...................#.................#.......................
..#.................................................................................#.#......................#....................
.......................#.............#..........#.......................#...................................#.....................
.................#......#......#...#.#.#.............................#.......................................................#....
..#...........................................................................................................#...................
.................................#..................#.....#........#.........................................................#....
.............#......................#...............................................#...#.............................#...#....#..
.#............................#..........................#.#...........................#..........................#.#.............
.............................#.........#.................##............................#...................#......................
.........#......#....#.........#..............................................................................................#..#
.....................#......#.................#..#..........................#..................#..............................#.#.
........................................................#......................#............#.......#......#......................
........................................#....##.#........#......#..............................#..........#..........#.##.........
.............#.......#.......#......................#..........................................#.........#............#...#...#.#.
..................#........................................................#...............................#.....#.....#..........
.........#.................#........#..............^..............#.................##......#...................................#.
....#...................#.#.................................#...#.................................................#...............
.................#.............................................#................#.....#......#.......#...................#........
...............#...............................................................................#..#...............#...............
........................................##..........................................................................##............
............................................#...................#........#...#...#....#.........#...................#.............
.....#...#...................#....................................#......#...........#...............#............................
........................#..................#.....................#.................................................#......##......
...#.................#....................#..................................#.#.........................#.......#................
....................#............#..................................#...#......#....................#...............#.............
.....#...............#.......###........#............#......................#.......#....#........................................
....................#........#...........#..................................#..#................#.............#...................
.....#..................#...................#.#..................................#.......#..#.....................................
..#..#.......#........................................#.................................................................#.........
.......#............#...#....#.......................#.#...................................#.........................#..#.#.......
..............#......................................................................................................#............
............#....................#........................#.........#...#.........................................................
...................................#..............................#.........................................................#.....
......##...........#.#........................#.........................................................................#.........
..........................#.....................................................#..............................#..............#...
.........................#...#.....................#..#................#................................................#....#....
...#.................................#...#.......#...............................................................#................
..................................#....................................#...........................##..#.#.....................#..
.#.............#.....#...............................................................#...............#...............#..#.........
.........#.............................#........................#.............#......................#........#............#......
...................................#..#...#.....................................#.................................................
........................................##..........#..............................................#.....................#........
......#...........................................#....#.....................#...................#..#.........#...................
.#....#.....#........##..................................#......#................................................#....#..##.......
..........#....................................................................##.........................#...........#...........
...........#.#......#.......#..................#............#...............................................#...#..............#..
.................................................................#.......#.........#.....#........#....##....#..........#.........
.............................#......................................................#.....#...#...#....#...........#..........#.#.
.....................#........#.........................#......................................#................#...............#.
.........................................................................#...................................................#....
..............#...#..#................#....................................#.......#....#.#...............#.........#.............
.......#......#.#............................................#.......#............................................................
##......................................................#..#.................................##...................#..#...........#
...........#.#...#........#............#..............................#........................#.........................#..#.....
................................................##.........................................#.................#....................
............................#....................................................#...#........................#.#......#........#.

View file

@ -18,6 +18,7 @@ fn main() -> std::io::Result<()> {
Ok(3) => solutions::day_3::run(args)?, Ok(3) => solutions::day_3::run(args)?,
Ok(4) => solutions::day_4::run(args)?, Ok(4) => solutions::day_4::run(args)?,
Ok(5) => solutions::day_5::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?"), Ok(_) => eprintln!("Unknown day. Are you from the future?"),
Err(_) => eprintln!("Unknown day format. Use integers to specify."), Err(_) => eprintln!("Unknown day format. Use integers to specify."),
} }

153
src/solutions/day_6.rs Normal file
View file

@ -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<Direction> 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<Field>> = 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(())
}

View file

@ -3,4 +3,5 @@ pub mod day_2;
pub mod day_3; pub mod day_3;
pub mod day_4; pub mod day_4;
pub mod day_5; pub mod day_5;
pub mod day_6;