day06: incomplete

This commit is contained in:
itsscb 2024-12-18 00:16:50 +01:00
parent e955977bab
commit 4f90390833
6 changed files with 347 additions and 21 deletions

View File

@ -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<i32, Vec<i32>>) -> Vec<i32> {
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;

86
src/day06/guard.rs Normal file
View File

@ -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<char> for Direction {
fn from(c: char) -> Self {
match c {
'^' => Self::Up,
'v' => Self::Down,
'<' => Self::Left,
'>' => Self::Right,
_ => Self::Unknown,
}
}
}

130
src/day06/input.txt Normal file
View File

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

109
src/day06/mod.rs Normal file
View File

@ -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<dyn Error>> {
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<char>]) -> i32 {
let mut obstacles: Vec<(usize, usize)> = vec![];
obstacles.len() as i32
}
fn calc_guard_path(guard: &mut Guard, map: &[Vec<char>]) {
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<Vec<char>>) {
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);
}
}

View File

@ -3,3 +3,4 @@ pub mod day02;
pub mod day03;
pub mod day04;
pub mod day05;
pub mod day06;

View File

@ -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::<String>("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::<String>("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::<String>("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::<String>("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::<String>("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::<String>("day06") {
match solve_day06(file) {
Ok((r1, r2)) => {
println!("Result of Day 05:\nPart one: {r1}\nPart two: {r2}");
}
Err(e) => eprintln!("{e}"),
}