From b67e546854cd6377389b4d68ab03f196bf204219 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Tue, 3 Dec 2024 22:01:46 +0100 Subject: [PATCH] feat: add day 13 p2 --- README.md | 3 +- src/bin/13.rs | 90 ++++++++++++++++++++++++++++----------------------- 2 files changed, 51 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 42a93a7..92b3212 100644 --- a/README.md +++ b/README.md @@ -41,8 +41,9 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www. | [Day 10](./src/bin/10.rs) | `164.5µs` | `287.9µs` | | [Day 11](./src/bin/11.rs) | `30.5s` | `-` | | [Day 12](./src/bin/12.rs) | `3.1ms` | `91.5ms` | +| [Day 13](./src/bin/13.rs) | `730.1µs` | `144.7µs` | -**Total: 47928.36ms** +**Total: 47929.23ms** --- diff --git a/src/bin/13.rs b/src/bin/13.rs index c80f643..07ea4cc 100644 --- a/src/bin/13.rs +++ b/src/bin/13.rs @@ -6,24 +6,31 @@ enum Type { Empty, } -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, Clone, Hash, Eq, Ord, PartialOrd)] struct Pos { x: u32, y: u32, + depth: usize, +} + +impl PartialEq for Pos { + fn eq(&self, other: &Self) -> bool { + self.x == other.x && self.y == other.y + } } impl Pos { - fn init(x: u32, y: u32) -> Self { - Self { x, y } + fn init(x: u32, y: u32, depth: usize) -> Self { + Self { x, y, depth } } fn origin() -> Self { - Self { x: 1, y: 1 } + Self { x: 1, y: 1, depth: 1 } } fn dest_test() -> Self { - Self { x: 7, y: 4 } + Self { x: 7, y: 4, depth: 0 } } fn dest() -> Self { - Self { x: 31, y: 39 } + Self { x: 31, y: 39, depth: 0 } } fn get_type(&self, nbr: u32) -> Type { let nb: u32 = @@ -37,13 +44,13 @@ impl Pos { fn neighbors(&self) -> Vec { let mut res: Vec = Vec::new(); if self.x > 0 { - res.push(Pos::init(self.x - 1, self.y)); + res.push(Pos::init(self.x - 1, self.y, self.depth + 1)); } if self.y > 0 { - res.push(Pos::init(self.x, self.y - 1)) + res.push(Pos::init(self.x, self.y - 1, self.depth + 1)) } - res.push(Pos::init(self.x + 1, self.y)); - res.push(Pos::init(self.x, self.y + 1)); + res.push(Pos::init(self.x + 1, self.y, self.depth + 1)); + res.push(Pos::init(self.x, self.y + 1, self.depth + 1)); res } fn empty_neighbors(&self, nbr: u32) -> Vec { @@ -92,33 +99,25 @@ fn take_n_steps(paths: Vec>, known: &mut Vec, nbr: u32, steps: usi } } -fn print_maze(path: &[Pos], nbr: u32) { - let origin = Pos::origin(); - let end = if nbr == 10 { - Pos::dest_test() - } else { - Pos::dest() - }; - for y in 0..50 { - for x in 0..80 { - let p = Pos::init(x, y); - if p == origin { - print!("S"); - } else if p == end { - print!("E"); - } else if path.contains(&p) { - print!("O"); - } else { - match p.get_type(nbr) { - Type::Wall => print!("#"), - Type::Empty => print!("."), - } - } - } - println!(); - } - println!(); -} +// use std::collections::HashMap; + +// fn print_maze(nbr: u32, known_store: &HashMap) { +// for y in 0..50 { +// for x in 0..50 { +// let p = Pos::init(x, y, 0); +// if let Some(depth) = known_store.get(&p) { +// print!("{:^4?}", depth); +// } else { +// match p.get_type(nbr) { +// Type::Wall => print!("####"), +// Type::Empty => print!(" "), +// } +// } +// } +// println!(); +// } +// println!(); +// } pub fn part_one(input: &str) -> Option { let nbr = input @@ -136,7 +135,6 @@ pub fn part_one(input: &str) -> Option { }; let path = search_for_pos(initial_path, &mut known, nbr, dest); - print_maze(&path, nbr); Some(path.len() - 1) } @@ -150,8 +148,18 @@ pub fn part_two(input: &str) -> Option { let initial_path = vec![vec![Pos::origin()]]; let mut known = vec![Pos::origin()]; - take_n_steps(initial_path, &mut known, nbr, 51); - print_maze(&known, nbr); + take_n_steps(initial_path, &mut known, nbr, 50); + + // let mut known_store: HashMap = HashMap::new(); + // for elem in &known { + // let mut pos = elem.clone(); + // pos.depth = 0; + // known_store.insert(pos, elem.depth); + // } + // print_maze(nbr, &known_store); + // println!("Known store: {:?}", known_store.len()); + known.sort(); + known.dedup(); Some(known.len()) } @@ -168,6 +176,6 @@ mod tests { #[test] fn test_part_two() { let result = part_two(&advent_of_code::template::read_file("examples", DAY)); - assert_eq!(result, None); + assert_eq!(result, Some(151)); } }