diff --git a/2022/day12/rust/Cargo.toml b/2022/day12/rust/Cargo.toml new file mode 100644 index 0000000..6f41f80 --- /dev/null +++ b/2022/day12/rust/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "rust_2022_12" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +aoc = { path = "../../../common/rust" } +petgraph = "0.6.2" diff --git a/2022/day12/rust/src/main.rs b/2022/day12/rust/src/main.rs new file mode 100644 index 0000000..f128bd1 --- /dev/null +++ b/2022/day12/rust/src/main.rs @@ -0,0 +1,93 @@ +#![warn(clippy::pedantic)] + +use std::io::{stdin, Read}; + +use petgraph::{ + algo::k_shortest_path, + graph::{Graph, NodeIndex}, + visit::IntoNodeReferences, +}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum NodeKind { + Start, + End, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +struct NodeData { + kind: Option, + height: u32, +} + +fn main() { + let mut data = String::new(); + stdin().read_to_string(&mut data).unwrap(); + + // graph of "downhill" edges - edge from A to B means that A is reachable from B + let mut graph = Graph::::new(); + + let grid: Vec> = data + .lines() + .map(|line| { + line.chars() + .map(|c| { + let (kind, c) = match c { + 'S' => (Some(NodeKind::Start), 'a'), + 'E' => (Some(NodeKind::End), 'z'), + c => (None, c), + }; + + let height = u32::from(c) - u32::from('a'); + let data = NodeData { kind, height }; + graph.add_node(data) + }) + .collect() + }) + .collect(); + + for (x, row) in grid.iter().enumerate() { + for (y, &node_id) in row.iter().enumerate() { + let node = graph[node_id]; + + let x = i32::try_from(x).unwrap(); + let y = i32::try_from(y).unwrap(); + let neighbours = [(-1i32, 0), (0, -1), (1, 0), (0, 1)] + .into_iter() + .filter_map(|(dx, dy)| { + let x = usize::try_from(x + dx).ok()?; + let y = usize::try_from(y + dy).ok()?; + grid.get(x)?.get(y) + }); + + for &neighbour_id in neighbours { + let neighbour = graph[neighbour_id]; + if node.height <= (neighbour.height + 1) { + // `node` is reachable from `neighbour` + graph.add_edge(node_id, neighbour_id, ()); + } + } + } + } + + let end = graph + .node_references() + .find_map(|(id, node)| (node.kind == Some(NodeKind::End)).then_some(id)) + .unwrap(); + let paths = k_shortest_path(&graph, end, None, 1, |_| 1); + + let start = graph + .node_references() + .find_map(|(id, node)| (node.kind == Some(NodeKind::Start)).then_some(id)) + .unwrap(); + println!("{:?}", paths[&start]); + + println!( + "{:?}", + paths + .into_iter() + .filter_map(|(start_id, steps)| (graph[start_id].height == 0).then_some(steps)) + .min() + .unwrap() + ); +} diff --git a/Cargo.lock b/Cargo.lock index 4a04c69..16da635 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -192,12 +192,24 @@ version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797" +[[package]] +name = "fixedbitset" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" + [[package]] name = "half" version = "1.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7" +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + [[package]] name = "hermit-abi" version = "0.1.19" @@ -207,6 +219,16 @@ dependencies = [ "libc", ] +[[package]] +name = "indexmap" +version = "1.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399" +dependencies = [ + "autocfg", + "hashbrown", +] + [[package]] name = "itertools" version = "0.10.5" @@ -320,6 +342,16 @@ version = "11.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575" +[[package]] +name = "petgraph" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6d5014253a1331579ce62aa67443b4a658c5e7dd03d4bc6d302b94474888143" +dependencies = [ + "fixedbitset", + "indexmap", +] + [[package]] name = "plotters" version = "0.3.4" @@ -545,6 +577,14 @@ dependencies = [ "aoc", ] +[[package]] +name = "rust_2022_12" +version = "0.1.0" +dependencies = [ + "aoc", + "petgraph", +] + [[package]] name = "ryu" version = "1.0.11" diff --git a/Cargo.toml b/Cargo.toml index 46ed02e..7cae5b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,4 +21,5 @@ members = [ "2022/day9/rust", "2022/day10/rust", "2022/day11/rust", + "2022/day12/rust", ]