mirror of
https://github.com/mx42/aoc2016.git
synced 2026-01-14 05:39:51 +01:00
feat: add day 13 p2
This commit is contained in:
@@ -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**
|
||||
<!--- benchmarking table --->
|
||||
|
||||
---
|
||||
|
||||
@@ -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<Pos> {
|
||||
let mut res: Vec<Pos> = 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<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) {
|
||||
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<Pos, usize>) {
|
||||
// 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<usize> {
|
||||
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);
|
||||
print_maze(&path, nbr);
|
||||
Some(path.len() - 1)
|
||||
}
|
||||
|
||||
@@ -150,8 +148,18 @@ pub fn part_two(input: &str) -> Option<usize> {
|
||||
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<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())
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user