feat: add day 13 p2

This commit is contained in:
Xavier Morel
2024-12-03 22:01:46 +01:00
parent 723001fd5b
commit b67e546854
2 changed files with 51 additions and 42 deletions

View File

@@ -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 10](./src/bin/10.rs) | `164.5µs` | `287.9µs` |
| [Day 11](./src/bin/11.rs) | `30.5s` | `-` | | [Day 11](./src/bin/11.rs) | `30.5s` | `-` |
| [Day 12](./src/bin/12.rs) | `3.1ms` | `91.5ms` | | [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**
<!--- benchmarking table ---> <!--- benchmarking table --->
--- ---

View File

@@ -6,24 +6,31 @@ enum Type {
Empty, Empty,
} }
#[derive(Debug, PartialEq, Clone)] #[derive(Debug, Clone, Hash, Eq, Ord, PartialOrd)]
struct Pos { struct Pos {
x: u32, x: u32,
y: 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 { impl Pos {
fn init(x: u32, y: u32) -> Self { fn init(x: u32, y: u32, depth: usize) -> Self {
Self { x, y } Self { x, y, depth }
} }
fn origin() -> Self { fn origin() -> Self {
Self { x: 1, y: 1 } Self { x: 1, y: 1, depth: 1 }
} }
fn dest_test() -> Self { fn dest_test() -> Self {
Self { x: 7, y: 4 } Self { x: 7, y: 4, depth: 0 }
} }
fn dest() -> Self { fn dest() -> Self {
Self { x: 31, y: 39 } Self { x: 31, y: 39, depth: 0 }
} }
fn get_type(&self, nbr: u32) -> Type { fn get_type(&self, nbr: u32) -> Type {
let nb: u32 = let nb: u32 =
@@ -37,13 +44,13 @@ impl Pos {
fn neighbors(&self) -> Vec<Pos> { fn neighbors(&self) -> Vec<Pos> {
let mut res: Vec<Pos> = Vec::new(); let mut res: Vec<Pos> = Vec::new();
if self.x > 0 { 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 { 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 + 1, self.y, self.depth + 1));
res.push(Pos::init(self.x, self.y + 1)); res.push(Pos::init(self.x, self.y + 1, self.depth + 1));
res res
} }
fn empty_neighbors(&self, nbr: u32) -> Vec<Pos> { fn empty_neighbors(&self, nbr: u32) -> Vec<Pos> {
@@ -92,33 +99,25 @@ fn take_n_steps(paths: Vec<Vec<Pos>>, known: &mut Vec<Pos>, nbr: u32, steps: usi
} }
} }
fn print_maze(path: &[Pos], nbr: u32) { // use std::collections::HashMap;
let origin = Pos::origin();
let end = if nbr == 10 { // fn print_maze(nbr: u32, known_store: &HashMap<Pos, usize>) {
Pos::dest_test() // for y in 0..50 {
} else { // for x in 0..50 {
Pos::dest() // let p = Pos::init(x, y, 0);
}; // if let Some(depth) = known_store.get(&p) {
for y in 0..50 { // print!("{:^4?}", depth);
for x in 0..80 { // } else {
let p = Pos::init(x, y); // match p.get_type(nbr) {
if p == origin { // Type::Wall => print!("####"),
print!("S"); // Type::Empty => print!(" "),
} else if p == end { // }
print!("E"); // }
} else if path.contains(&p) { // }
print!("O"); // println!();
} else { // }
match p.get_type(nbr) { // println!();
Type::Wall => print!("#"), // }
Type::Empty => print!("."),
}
}
}
println!();
}
println!();
}
pub fn part_one(input: &str) -> Option<usize> { pub fn part_one(input: &str) -> Option<usize> {
let nbr = input let nbr = input
@@ -136,7 +135,6 @@ pub fn part_one(input: &str) -> Option<usize> {
}; };
let path = search_for_pos(initial_path, &mut known, nbr, dest); let path = search_for_pos(initial_path, &mut known, nbr, dest);
print_maze(&path, nbr);
Some(path.len() - 1) Some(path.len() - 1)
} }
@@ -150,8 +148,18 @@ pub fn part_two(input: &str) -> Option<usize> {
let initial_path = vec![vec![Pos::origin()]]; let initial_path = vec![vec![Pos::origin()]];
let mut known = vec![Pos::origin()]; let mut known = vec![Pos::origin()];
take_n_steps(initial_path, &mut known, nbr, 51); take_n_steps(initial_path, &mut known, nbr, 50);
print_maze(&known, nbr);
// let mut known_store: HashMap<Pos, usize> = 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()) Some(known.len())
} }
@@ -168,6 +176,6 @@ mod tests {
#[test] #[test]
fn test_part_two() { fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY)); let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, None); assert_eq!(result, Some(151));
} }
} }