day 12 solution
parent
59498dd72c
commit
b433dadf5f
|
@ -13,4 +13,43 @@ name = "aoc2022"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"petgraph",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||
|
||||
[[package]]
|
||||
name = "fixedbitset"
|
||||
version = "0.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.12.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
|
||||
|
||||
[[package]]
|
||||
name = "indexmap"
|
||||
version = "1.9.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"hashbrown",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "petgraph"
|
||||
version = "0.6.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6d5014253a1331579ce62aa67443b4a658c5e7dd03d4bc6d302b94474888143"
|
||||
dependencies = [
|
||||
"fixedbitset",
|
||||
"indexmap",
|
||||
]
|
||||
|
|
|
@ -11,3 +11,4 @@ test_input = []
|
|||
|
||||
[dependencies]
|
||||
anyhow = "1.0.66"
|
||||
petgraph = "0.6.2"
|
||||
|
|
File diff suppressed because it is too large
Load Diff
Binary file not shown.
After Width: | Height: | Size: 417 KiB |
|
@ -0,0 +1,41 @@
|
|||
abcccccccccccccccccaaccccccccccccaaaaaaaacccccccccccaaaaaccccaaaaaaccaaaaaaaaaaaaaaaaaccccccccccccccccaaacccccaaaaaaaacccaaaccccccccccccccccccccccccccccccccccccccccccccccccccaaaaa
|
||||
abccccccccccccccccaaacaacccccccccccaaaacccccccccccccaaaaaacccaaaaaaccaaaaaaaaaaaaaaaaaaaacccccccccaaacaaacccccaaaaaaaaaccaaaaccccccccccccccccccccccccccccccccccccccccccccccccaaaaaa
|
||||
abcccccccccccccccccaaaaacccccccccccaaaaaccccccccccccaaaaaaccccaaaacccaaaacccaaaaaaaaaaaaacccccccccaaaaaaaaaacccaaaaaaaaccaaaaccccccccccccccccccccccccccccccccccaaacccccccccccaaaaaa
|
||||
abcccccccccccccccaaaaaacccccccccccaaacaaccaaccccccccaaaaaaccccaaaacccaaaccccaaaaaaaaaaaaaccccccccccaaaaaaaaaccaaaaaacccccaaacccccccccccccccccccccccccccccccccccaaaccccccccccccccaaa
|
||||
abcccccccccccccccaaaaaaaacccccccccaacccacaaacaacccccccaaccccccaccaccccccccaaaaaaaaaaaaccccccccccccccaaaaaaacccaaaaaaacccccccccccccaacccccccccccccccccccccccccccaaaccccccccccccccaaa
|
||||
abcccccccccccccccaacaaaaacccccccccccccccccaaaaacccccccccccccccccccccccccccaaaaaaaaaaaaccccccccccccccaaaaaaccccaaccaaacccccccccccaaaaaaccccccccccccccccccccccccccdccccccccccccccccaa
|
||||
abccaacccccccaaacccaaacaccccccccccaaacccaaaaaaccccccccccccccccccccccccccccaaacccaaaaaacaaaaccccccccaaaaaaaccccccccaaacccccccccccaaaaaacccccccccccccccccllllllcccdddddcccccccccccccc
|
||||
abaaaacccacccaaccccaacccccccccccccaaacccaaaaaaaacccccccccccccccccaaccccccccacccaaaaccccaaaaccccccccaaacaaaccccccccccccccccccccccaaaaaaccccccccccccccccllllllllldddddddddddccaaccccc
|
||||
abaaaaccaaaaaaaacccccccccccccccaaaaaaaacaacaaaaacccccccccccccccccaaacccccccaaacaaccccccaaaacccccccccccccaaccccccccccccccccccccccaaaaaccccccccccccccccclllllllllldddddddddeeaaaccccc
|
||||
abaaaccccaaaaaaaaccccccccaaacccaaaaaaaacccaaacccccccccccccccccaaaaaaaaccccccaaaaacccccccaacccccccccccccccccccccccccccccccccccccccaaaaccccaaaccccccccckllppppplllmmmmmmmdeeeeaaccccc
|
||||
abaaaacccaaaaaaaaacccccaaaaaaccccaaaaaccccaaccccccccccccccccccaaaaaaaaccccccaaaaaaacccccccccccccccccccccccccccccccccccccccccccccccccccccaaaacccccccckklpppppppplmmmmmmmmmeeeeaccccc
|
||||
abaaaacccaaaaaaaaacccccaaaaaacccaaaaaaccccccccaacccccccccccccccaaaaaaccccccaaaaaaaacccccccccccccccccccccccccccccccccccccccccccccccccccccaaaacccccccckkkppppppppqmmmmmmmmmmeeeaacccc
|
||||
abaaaaaccaaaaaaaaccccccaaaaaacccaaaaaaccccccacaaaacccccccccccccaaaaaaccccccaaaaaaaaccccccccaaaaccccccccaaacccccccccccccccccccccccccccccccaaacccccccckkkpppuuuppqqqqqqqqmmmeeeeacccc
|
||||
abacccccaaaaaaaccccccccaaaaaccccaccaaaccccccaaaaaacccccacccccccaaaaaaccccccaaaaaacaaaccccccaaaaccccccccaaaacccccccccccccccccccccccccccccccccccccccckkkpppuuuuuuqqqqqqqqqnnneeeccccc
|
||||
abcccccccaccaaaccccccccaaaaacccccccccccccccccaaaacccaaaacccccccaacaaacccccccccaaacaaaaaccccaaaaccccccccaaaaccccccccccccccccccccccccccccccccccccccckkkkpppuuuuuuuqvvvvqqqnnneeeccccc
|
||||
abcccccccccccaaacaaccccccccccccccccccccccccccaaaacccaaaaaacccccccccccccccccccccccaaaaaaccccaaacccccccccaaaccccccccccccccccccccccccccccccccccccccckkkkrrpuuuxxxuvvvvvvvqqnnneeeccccc
|
||||
abcccccccccccccccaacaaaccccccccccccccccccccccaacaacccaaaaacccccccccccccccccccccccaaaaaaccccccccccccccccccccccccccccccccccccccccccccccccccccccccckkkkrrrruuxxxxuvvvvvvvqqnnneeeccccc
|
||||
abcccccccccccccccaaaaacccccccccccccccccccccccccccaccaaaaacccccaaccccccccccccccccccaaaaaccccccccccccccccaaaccccccccccccccaaacccccccccccccccccccckkkkrrrruuuxxxxyyyyyvvvqqnnneecccccc
|
||||
abcccccccccccccaaaaaacccccccccccaacaacccccccccccaaaaaaaaacccacaaaacaaccaaccccccccaaacaaccccccccccaaccccaaaaaacccaacaacccaaacacccccccccccccccccjjkkrrrruuuuuxxxyyyyyvvqrqnneffcccccc
|
||||
abcccccccccccccaaaaaaaacccaaacccaaaaaccccccaacccaaaaaaaaacccaaaaaacaaaaaaccccccccaaacaaacccccccaaaaaacaaaaaaacccaaaaacaaaaaaaaccccccccccccccccjjjrrrtttuuxxxxxyyyyyvvrrnnnfffcccccc
|
||||
SbccccccccccccccccaaaaacccaaaaccaaaaaaccccaaaaaacaaaaaaaaacccaaaacccaaaaaccccccccaaaaaaacccccccaaaaaaaaaaaaaaccaaaaaccaaaaaaaaccccccccccccccccjjjrrrtttxxxEzzzzyyyvvrrrnnnfffcccccc
|
||||
abccccccccccaaaccaaccaacccaaaaccaaaaaacccccaaaaacaaaaaaaaacccaaaaccaaaaaacccccccccaaaaaaccccccccaaaacaaaaaaacccaaaaaaccaaaaaacccccccccccccccccjjjrrrtttxxxxxyyyyyyvvrrrnnnfffcccccc
|
||||
abcccccccccaaaaccaacccccccaaacccaaaaaacccaaaaaaaaaaaaaaaaacccaacacaaaaaaaacccccaaaaaaaacccccccccaaaacccaaaaaaccccaaaacccaaaaacccccccccccccccccjjjrrrtttxxxxxyyyyyyywvrrnnnfffcccccc
|
||||
abcccccccccaaaacccccccccccccccccccaaaccccaaaaaaaaaaaaaaaaaacccccccaaaaaaaacccccaaaaaaaaaccccccccaccacccaaaaaaccccacccccaaaaaacccccccccccccccccjjjrrrrttttxxxyyyyyyywwrrroooffcccccc
|
||||
abccccccccccaaaccccccccccccccccccccccccccaaaaaaaaaccaaaaaaaccccccccccaaccccccccaaaaaaaaaaccccccccccccccaacccccccccccccccaaccccccccccccccccccccjjjjqqqqttttxxyywwwwwwwwrrooofffccccc
|
||||
abcccccccccccccccccccccccccccccccccccccccccaaacacccccaaaaccccccccccccaacccccccccccaaacaaacccccccccccccccccccccccccccccaaacccccccccccccccccccaacjjjjqqqqqttwwwwwwwwwwwrrrooofffccccc
|
||||
abcccccccccccccccccccccccccccccccccccccccccaaccccccccccaacccccccccccccccccccccccccaaacccccccccccccccccccccccccccccccccaaacccccccccccccccccaaaaaajjjjqqqqttwwwwwwsswwrrrrooofffccccc
|
||||
abcccccaaaaccccccccccccccaacaaccccccccccccccccccccccccccccccccccccccccccccccccccccaacccccccccccccccccccccccccccccccaaaaaaaaccaaaacccccccccaaaaaacjjjiqqqtttwwwwsssssrrrrooofffccccc
|
||||
abccccaaaaaccccccccccccccaaaaacccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccaaaaaaaaccaaaaacccccccccaaaaacciiiiqqttswwwssssssrrroooogffccccc
|
||||
abccccaaaaaacccccccccccccaaaaaacccaaaccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccaaaaaccaaaaaaccccccccaaaaacccciiiqqqssssssspppooooooogggaccccc
|
||||
abccccaaaaaaccccccccaacccaaaaaaccccaacccccccccccccccccccccccccccccccccaaaaccccccccccccccccccccccccccccccccccccccccccaaaaaaccaaaaaaccccccccaaaaaccccciiiqqsssssspppppoooooggggaacccc
|
||||
abccccaaaaaccccccaaaaacccaaaaaacaacaaaaaccccccccccccccccccccaacccccccaaaaacccccccccccaaaccccccccccccccccccccccccccccaaaaaacccaaaaacccccccccacccccccciiiqqqpssspppppgggggggggaaacccc
|
||||
abccccccaaacccccccaaaaaccccaaaccaaaaaaaacccccccaacccccccaaccaacccccccaaaaaacccccccccaaaacccccccccccaaaaccccccccccccaaccaaacccaaacccccaaacaaaccccccccciiqqppppppphhhggggggggaaaacccc
|
||||
abccccccccccccccccaaaaaccccccccccaaaaaccccccccaaacaaccccaaaaaacccccccaaaaaaaccccccccaaaacccccccccccaaaacccccccccaaaaaacccccccccccccccaaaaaaaccccccccciiippppppphhhhggggggcaaacccccc
|
||||
abaacccccccccccccaaaaaccccccccccccaaaaaccccccccaaaaacccccaaaaaaacccccaaaaacaaacccccccaaacccccccccccaaaacccccccccaaaaacccccccccccccccccaaaaaacccccccccciiiippphhhhhhcccccccaaacccccc
|
||||
abaacccccccccccccccaaacccccccccccaaacaaccccccaaaaaaccccccaaaaaaacccaaccaaacaaaaccaaaccccccccccccccccaaacccccccccaaaaaaacccccccccccccccaaaaaaaacccccccciiihhhhhhhhaaaccccccccccccccc
|
||||
abaaccccccccccccccccccccccccccccccaacccccccccaaaaaaaacccaaaaaaccaaaaaacccccaaaacaaaaaccccccccccccccccccccccccccaaaaaaaaccccccccccccccaaaaaaaaaccccccccciihhhhhhcaaaacccccccccccccca
|
||||
abaaccccccccccccccccccccccccccccccccaacccccccaacaaaaacccaaaaaaccaaaaacccccccaaaaaaaacccccccccccccccccccccccccccaaaaaaaacccccccccaaacaaaaaaaaaacccccccccccchhhaccccaacccccccccccccca
|
||||
abaaccccccccccccccccccccccccccccccccaaaaaaccccccaaccccccccccaaccaaaaaaacccccaaaaaaaccccccccccccccccccccccccaaaccacaaacccccccccccaaaaaaacaaacaaaaaacccccccccaaacccccccccccccccaaaaaa
|
||||
abccccccccccccccccccccccccccccccccccaaaaaccccccaaccccccccccccccaaaaaaaacaaaaaaaaaaccccccccccccccccccccccccaaaaaaccaaaccccccccccccaaaaaacaaacaaaaaacccccccccaaaccccccccccccccccaaaaa
|
||||
abccccccccccccccccccccccccccccccccaaaaaaaacccccccccccccccccccccaaaaaaaacaaaaaaaaaaaaacccccccccccccccccccccaaaaaacccccccccccccccaaaaaaaaaaaaaaaaaacccccccccccccccccccccccccccccaaaaa
|
|
@ -0,0 +1,141 @@
|
|||
use anyhow::{anyhow, Result};
|
||||
use petgraph::algo::astar;
|
||||
use petgraph::dot::{Config, Dot};
|
||||
use petgraph::graph::{DefaultIx, NodeIndex};
|
||||
use petgraph::prelude::*;
|
||||
//use petgraph::{Directed, Graph};
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
|
||||
type Map = [[i32; 180]; 180];
|
||||
type WGraph = Graph<Coord, i32, Directed, DefaultIx>;
|
||||
type Coord = (usize, usize);
|
||||
type Indexes = HashMap<Coord, NodeIndex<DefaultIx>>;
|
||||
|
||||
pub fn run() -> Result<()> {
|
||||
#[cfg(not(feature = "test_input"))]
|
||||
let file_contents = fs::read_to_string("day12.txt")?;
|
||||
|
||||
#[cfg(feature = "test_input")]
|
||||
let file_contents = fs::read_to_string("tests/day12.txt")?;
|
||||
|
||||
let mut world: Map = [[0; 180]; 180];
|
||||
let mut rows = 0;
|
||||
let mut cols = 0;
|
||||
let mut graph: WGraph = Graph::new();
|
||||
let mut indexes: Indexes = HashMap::with_capacity(180 * 180);
|
||||
let mut start: Coord = (0, 0);
|
||||
let mut end: Coord = (0, 0);
|
||||
let mut p2starts: Vec<NodeIndex<DefaultIx>> = Vec::new();
|
||||
|
||||
for line in file_contents.lines() {
|
||||
cols = 0;
|
||||
for char in line.chars() {
|
||||
world[rows][cols] = get_height(&char);
|
||||
let index = graph.add_node((rows, cols));
|
||||
if world[rows][cols] == 1 {
|
||||
p2starts.push(index);
|
||||
}
|
||||
indexes.insert((rows, cols), index);
|
||||
if char == 'S' {
|
||||
start = (rows, cols);
|
||||
} else if char == 'E' {
|
||||
end = (rows, cols);
|
||||
}
|
||||
cols += 1;
|
||||
}
|
||||
rows += 1;
|
||||
}
|
||||
println!("finding distance between {:?} and {:?}", start, end);
|
||||
|
||||
for x in 0..rows {
|
||||
for y in 0..cols {
|
||||
let cur = (x, y);
|
||||
if cur == end {
|
||||
continue;
|
||||
}
|
||||
if x > 0 {
|
||||
check_coord(&mut graph, &world, &indexes, &cur, &(x - 1, y));
|
||||
}
|
||||
if x < rows - 1 {
|
||||
check_coord(&mut graph, &world, &indexes, &cur, &(x + 1, y));
|
||||
}
|
||||
if y > 0 {
|
||||
check_coord(&mut graph, &world, &indexes, &cur, &(x, y - 1));
|
||||
}
|
||||
if y < cols - 1 {
|
||||
check_coord(&mut graph, &world, &indexes, &cur, &(x, y + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let dotpath = Path::new("day12.dot");
|
||||
let mut file = match File::create(&dotpath) {
|
||||
Err(why) => return Err(anyhow!("could not write file {}", why)),
|
||||
Ok(file) => file,
|
||||
};
|
||||
match file
|
||||
.write_all(format!("{:?}", Dot::with_config(&graph, &[Config::EdgeNoLabel])).as_bytes())
|
||||
{
|
||||
Err(why) => return Err(anyhow!("could not write dot file {}", why)),
|
||||
Ok(_) => println!("wrote day12.dot"),
|
||||
};
|
||||
|
||||
let start_index = indexes.get(&start).unwrap();
|
||||
let end_index = indexes.get(&end).unwrap();
|
||||
#[allow(unused_variables)]
|
||||
if let Some((len, path)) = astar(
|
||||
&graph,
|
||||
*start_index,
|
||||
|finish| finish == *end_index,
|
||||
|e| *e.weight(),
|
||||
|_| 0,
|
||||
) {
|
||||
println!("part one steps ({})", len);
|
||||
#[cfg(feature = "test_input")]
|
||||
for n in path.iter() {
|
||||
println!("{:?}", graph.node_weight(*n).unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
let mut min_len: i32 = rows as i32 * cols as i32;
|
||||
for s in p2starts.iter() {
|
||||
if let Some((len, _)) = astar(
|
||||
&graph,
|
||||
*s,
|
||||
|finish| finish == *end_index,
|
||||
|e| *e.weight(),
|
||||
|_| 0,
|
||||
) {
|
||||
if len < min_len {
|
||||
min_len = len;
|
||||
}
|
||||
}
|
||||
}
|
||||
println!("part 2 minimum steps {}", min_len);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn check_coord(graph: &mut WGraph, world: &Map, indexes: &Indexes, source: &Coord, dest: &Coord) {
|
||||
let h = world[source.0][source.1];
|
||||
let hi = indexes.get(source).unwrap();
|
||||
let d = world[dest.0][dest.1];
|
||||
let di = indexes.get(dest).unwrap();
|
||||
|
||||
if (d > h && d == h + 1) || d == h || d < h {
|
||||
graph.update_edge(*hi, *di, 1);
|
||||
}
|
||||
}
|
||||
|
||||
fn get_height(c: &char) -> i32 {
|
||||
if *c == 'S' {
|
||||
1
|
||||
} else if *c == 'E' {
|
||||
27
|
||||
} else {
|
||||
*c as i32 - 96
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
pub mod day1;
|
||||
pub mod day10;
|
||||
pub mod day11;
|
||||
pub mod day12;
|
||||
pub mod day2;
|
||||
pub mod day3;
|
||||
pub mod day4;
|
||||
|
|
|
@ -45,6 +45,9 @@ fn run_day(number: i32) -> Result<()> {
|
|||
11 => {
|
||||
days::day11::run()?;
|
||||
}
|
||||
12 => {
|
||||
days::day12::run()?;
|
||||
}
|
||||
_ => return Err(anyhow!("Invalid day provided")),
|
||||
}
|
||||
Ok(())
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
Sabqponm
|
||||
abcryxxl
|
||||
accszExk
|
||||
acctuvwj
|
||||
abdefghi
|
Loading…
Reference in New Issue