From fb2a96f435b89d7e791f898263cc19cadfa40bc9 Mon Sep 17 00:00:00 2001 From: Ashhhleyyy Date: Mon, 12 Dec 2022 12:51:33 +0000 Subject: [PATCH] feat: day 12 (over 1h) --- .aoc-cache/12.txt | 41 +++++++++++++ Cargo.lock | 39 ++++++++++++ Cargo.toml | 1 + src/bin/day_12.rs | 138 ++++++++++++++++++++++++++++++++++++++++++ test_cases/day_12.txt | 5 ++ 5 files changed, 224 insertions(+) create mode 100644 .aoc-cache/12.txt create mode 100644 src/bin/day_12.rs create mode 100644 test_cases/day_12.txt diff --git a/.aoc-cache/12.txt b/.aoc-cache/12.txt new file mode 100644 index 0000000..7f1b7da --- /dev/null +++ b/.aoc-cache/12.txt @@ -0,0 +1,41 @@ +abcccccccccccaaaaacccccccaaaaaaccccccccccccccccccccccccccccccccaaaaaaaaaaaaaacccccccccccccccccccaaaaaacccccccacccccccccaaccaaccccccccccccccccccccccccccccccaaaaaa +abcccccccccccaaaaaaacccccaaaaaaccccccccaaccccccccccaaaaccccccccaaaaaaaaaaaaaccccccccccccccccccccaaaaaaccccaaaacccccccccaaaaaaccccccccccccccccccccccccccccccccaaaa +abccccccccccaaaaaaaacccccaaaaaccccccccaaaacccccccccaaaacccccccccccaaaaaaacccccccccccccccccccccccaaaaacccccaaaaaaccccccccaaaaacccccccccccaaaccccccccccccccccccaaaa +abccccccccccaaaaaaaaccccccaaaaacccccccaaaacccccccccaaaacccccaaaccccaaaaaaccccccccccccccccccccccccaaaaacccccaaaaacccccccaaaaaacccccccccccaaaacccccccccccccccccaaaa +abccccccccccaaaaaaccccccccaaaaacccccccaaaaccccccccccaaacccccaaaaccaaaaaaaacccccccccccccccccccccccaaaaaccccaaaaacccccccaaaaaaaaccccccccccaaaaccaaccccccccccccaaaaa +abcccccccccccccaaaccccccccccccccccccccccccccccccccccccccccccaaaaccaaaaaaaaccccccccccccccccccccccccccccccccaccaaccccaaaaaaaaaaacccccccccccaaaaaaccccccccccccccaccc +abcacccccccccccccaaaccccccccccccccaaacccccccccccccccccccccccaaaccaaaacccaaacccccccccccccccccccccccaacccccccccccccccaaacccaaccccccccccccccaaaalllllccccccccccccccc +abaacccccccccccccaaaaaacccccccccccaaaccccccccccccccccccccccccccccaaaccccaaaccccccccccccccccccccccaaccccccccccccccccaaaaaaaaccccccccccccccckklllllllccccaaaccccccc +abaaaaacccccccccaaaaaaacccccccccaaaaaaaacccccaacccccccccccccccccccccccccaaaccccccccaacccccccccaaaaacaacaacaacccccaaaaaaaaccccccccccccaaakkkkllllllllcccaaaccccccc +abaaaaaccccccccaaaaaaaacccccccccaaaaaaaacccccaaaaaacccccccccccccccccccccaaaccccccaaaacacccccccaaaaaaaacaaaaaccccaaaaaaaaaccccccccckkkkkkkkkklsssslllcccaaaaaacccc +abaaaccccccccccaaaaaaacccccccccccaaaaacccccccaaaaaaccccccccccccccccccaaaaaaaaccccaaaaaacccccccccaaaaacaaaaacccccaaaaaaaacccaaaccjjkkkkkkkkkssssssslllcccaaaaacccc +abaaaccccccccccccaaaaaacccccaacccaaaaaaccccaaaaaaaccaaaccccccccccccccaaaaaaaacccccaaaacccccccccaaaaaccaaaaaacccccccaaaaaaccaaaajjjjkkkkkkssssssssslllcddaaaaccccc +abcaaacccccccccccaaaaaacaaacaacccaaaaaaccccaaaaaaaaaaaaaaccccccccccccccaaaaaccccccaaaaccccaaaaaaacaaacccaaaaaaacccaaaaaaaccaaajjjjrrrrrrssssuuuussqmmddddaaaccccc +abccaacccccccccccaaccccccaaaaacccaaaccaccccaaaaaaaaaaaaaacccccccccccccaaaaaacccccaacaaccccaaaaacccaaccccacccaaaaaaaaaccaaccaaajjjrrrrrrrrssuuuuuvqqmmmdddaaaccccc +abccccccccaaccccccccccccccaaaaaacccccccccccccaaaaaaaaaaaccccccccccccccaaaaaacccccccccccccaaaaaaccccccccccccaaaaaaaaaacccccccccjjjrrruuuuuuuuuuuvvqqmmmmddddaccccc +abaacccccaaaaccccccccccccaaaaaaacccccccccccccaacccccaaaaacccccccccccccaccaaacccccccccccccaaaaaacccccccccccaaaaaaaaccccccccccccjjjrrruuuuuuuuxyyvvqqqmmmddddcccccc +abaacccccaaaacccccccccccaaaaaaccccccccaaccccccccacccaaaaacccccccccccccccccccccccccccccccccaaaaacccccccccccaaaaaaacccccccccccccjjjrrttuxxxxuxxyyvvqqqqmmmmddddcccc +abaacccccaaaacccccccccccaacaaacccccaaaaacccaaaaaaaccccccccccccccccccccccccccccccccccccccccaaacccccccccccccccaaaaaaccccccccccccjjjrrtttxxxxxxyyyvvqqqqqmmmmdddcccc +abacccccccccccccccccccccccccaaccccccaaaaaccaaaaaaaccccccccaaccccccccccccccccccccccccccccccccccccccccccccccccaaaaaaccccccccccccjjjqrrttxxxxxxyyyvvvvqqqqmmmdddcccc +abccccccccccccccccccccccccccccccccccaaaaaccaaaaaaacccccccaaaccccccccccaaaccccccccccccccccccaaaccccccccccccccaaccccccccccaaccccjjjqqqtttxxxxxyyyyyvvvqqqqmmmeeeccc +SbaaccccccaccaaacccccccccccccccccccaaaaacccaaaaaaaaccccaaaaaaaacccccccaaacaaccccccccccccccaaaaaaccccccccccccccccccccccaaaaaaccciiiqqqttxxxxEzyyyyyvvvqqqnnneeeccc +abaaccccccaaaaaaccccccccccccccccccccccaaccaaaaaaaaaacccaaaaaaaacccccaaaaaaaaccccccccccccccaaaaaaccccccccccccccccccccccaaaaaaccciiiqqtttxxxyyyyyyyvvvvqqqnnneeeccc +abaaaaacccaaaaaaccccccccccccccccccccccccccaaaaaaaaaaccccaaaaaaccccccaaaaaaaaccccccccccccccaaaaaacccccccccccccccccccccccaaaaacciiiqqqttxxyyyyyyywvvvvrrrqnnneeeccc +abaaaaacccaaaaaaaccccccccaaaccccccccccccccaacaaaccccccccaaaaaaccccccaaaaaaaccccccccccccccccaaaaaccccccccccccccccccccccaaaaaccciiiqqtttxxxyyyyywwwvrrrrrnnneeecccc +abaaaccccaaaaaaaaccccccccaaaacccccccccccccccccaaccccccccaaaaaaccccccccaaaaaccccccccccccccccaacaaccccccccccccccccccccccaaaaaccciiqqqttxxxwwwwyyywwrrrrnnnnneeecccc +abaaaccccaaaaaaaaccccccccaaaacccccaaaaacccccccaaccccccccaaccaacccccccaaacaaacccccccccccccccccccccccccccccccccccccccccccccccccciiqqqtttwwwwwwywwwrrrrnnnnneeeccccc +abcaaaccccccaaaccccccccccaaaccccccaaaaacccccccccccccaaaaccccccccccccccaacccccccccccccccccccccccccccccccaaaacccccccccccccccccciiiqqqtttssssswwwwwrrrnnnneeeecccccc +abccaaccccccaaaccccccccccccccccccaaaaaacccccccccccccaaaaccccccccccccccccccccccccccccccccccccccccccccccaaaaacccccccccccccccccciiiqqqqtssssssswwwwrronnnfeeeacccccc +abcccccccccccccccccccccccccccccccaaaaaacccccccccccccaaaaccccccccccccccccccccccccccccccccccccccccccccccaaaaaaccccccccccccccccciiiiqqppssssssswwwwrroonfffeaacccccc +abcccccccccccccccccccccccccccccccaaaaaacaaaccccccccccaacccccccccccccccccccccccccccccccccaaacccccccccccaaaaaaccccccccccccccccccihhpppppppppsssssrrroonfffaaaaacccc +abcccccccccccccccccccccccccccccccccaacccaaaaacccccccccccccccccccccccccccccccccccccccccccaaaaaaccccccccaaaaaccccccccccccccccccchhhhppppppppppssssrooofffaaaaaacccc +abccccccccaaaccccccccccccccccccccccccccaaaaacccccccccccccccccccccccccccccccccccccaccccaaaaaaaaccccccccccaaacccccccccccccccccccchhhhhhhhhpppposssoooofffaaaaaccccc +abccccccccaaaacccccaaccccccccccccccccccaaaaaccccccccccccccccccccccccaaccccccccccaaccccaaaaaaaacccccccccccccccccccccccccccccccccchhhhhhhhhgppooooooofffaaaaacccccc +abaaccccccaaaacccccaacaaccccccccccccccccaaaaacccccccccccccccaacaaccaaaaaaccccaaaaacaacaaaaaaacccccccccccccccccccccccccccccccccccccchhhhhhgggooooooffffaaaaaaccccc +abaaacccccaaaacccccaaaaaccccccccccccccccaaccccccccccccccccccaaaaaccaaaaaaccccaaaaaaaacccaaaaaccccccccccccccccccccccccaaacaacccccccccccccgggggooooffffccccaacccccc +abaaaccccccccccccaaaaaaccccaaccccccccccccccccccaaacccccccccccaaaaaaaaaaaacaacccaaaaaccccaacaaacccccccccccccccccccccccaaaaaaccccccccccccccaggggggggfffcccccccccccc +abaaaccccccccccccaaaaaaaacaaaaccccccccaaaccccccaaaacccccccccaaaaaaaaaaaaaaaaccaaaaacccccaaaaaccccccccccccccaaccccccccaaaaaaccccccccccccccaagggggggfccccccccccccca +abaacccccccccccccaccaaaaacaaaaccccccccaaaccccccaaaacccccccccaaaaccaaaaaaaaaaccaacaaaccccccaaaccccccccccccaaaaaacccccaaaaaaacccccccccccccaaaaccggggcccccccccccccaa +abaacccccccccccccccaaacaccaaaacccccaaaaaaaaccccaaaccccccccccccaaccaaaaaaaacccccccaacccccaacaaaaacccccccccaaaaaacccccaaaaaaaaccccccccccccaaaccccccccccccccccaaacaa +abcccccccccccccccccaaccccccccccccccaaaaaaaaccccccccccccccccccccaaaaaaaaaaaccccccccccccccaaaaaaaacccccccccaaaaaacccccaaaaaaaaccccccccccccacaccccccccccccccccaaaaaa +abccccccccccccccccccccccccccccccccccaaaaaacccccccccccccccccccccaaaaaaaaaaaaccccccccccccccaaaaaaccccccccccaaaaaccccccccaaacccccccccccccccccccccccccccccccccccaaaaa diff --git a/Cargo.lock b/Cargo.lock index f89ed33..1fda254 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,6 +32,7 @@ version = "0.1.0" dependencies = [ "aoc_proc", "color-eyre", + "petgraph", "regex", "time", "ureq", @@ -45,6 +46,12 @@ dependencies = [ "syn", ] +[[package]] +name = "autocfg" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" + [[package]] name = "backtrace" version = "0.3.66" @@ -136,6 +143,12 @@ dependencies = [ "once_cell", ] +[[package]] +name = "fixedbitset" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" + [[package]] name = "flate2" version = "1.0.25" @@ -161,6 +174,12 @@ version = "0.26.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "22030e2c5a68ec659fde1e949a745124b48e6fa8b045b7ed5bd1fe4ccc5c4e5d" +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + [[package]] name = "idna" version = "0.3.0" @@ -177,6 +196,16 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683" +[[package]] +name = "indexmap" +version = "1.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399" +dependencies = [ + "autocfg", + "hashbrown", +] + [[package]] name = "js-sys" version = "0.3.60" @@ -258,6 +287,16 @@ version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e" +[[package]] +name = "petgraph" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6d5014253a1331579ce62aa67443b4a658c5e7dd03d4bc6d302b94474888143" +dependencies = [ + "fixedbitset", + "indexmap", +] + [[package]] name = "pin-project-lite" version = "0.2.9" diff --git a/Cargo.toml b/Cargo.toml index 26d689b..aa7e239 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,3 +14,4 @@ aoc_proc = { path = "aoc_proc" } ureq = "2.5.0" time = "0.3.17" regex = "1.7.0" +petgraph = "0.6.2" diff --git a/src/bin/day_12.rs b/src/bin/day_12.rs new file mode 100644 index 0000000..dad56d6 --- /dev/null +++ b/src/bin/day_12.rs @@ -0,0 +1,138 @@ +use std::fmt::Debug; + +use petgraph::dot::{Config, Dot}; +use petgraph::graph::{NodeIndex, DiGraph}; +use petgraph::algo::dijkstra; + +use aoc_2022::prelude::*; + +type Input = Vec>; + +fn parse(s: &str) -> Result { + Ok(s.lines().map(|line| line.chars().collect()).collect()) +} + +#[aoc(day = 12, parse = parse, test_cases = ["day_12.txt"])] +fn day_12(input: Input) -> Result<()> { + let map_w = input[0].len(); + let map_h = input.len(); + + let (graph, (start, end)) = build_graph((map_w, map_h), &input); + if std::env::var("PRINT_GRAPH").is_ok() { + println!("{:?}", Dot::with_config(&graph, &[Config::EdgeNoLabel])); + } + + // Part 1 + let map = dijkstra(&graph, Pos(start.0,start.1).into(), Some(Pos(end.0,end.1).into()), |_| 1); + eprintln!("Part one: {}", map.get(&Pos(end.0,end.1).into()).unwrap()); + + // Part 2 + let starts = find_starts((map_w, map_h), &input); + let min = starts.into_iter().filter_map(|start| { + let map = dijkstra(&graph, start.into(), Some(Pos(end.0,end.1).into()), |_| 1); + map.get(&Pos(end.0,end.1).into()).cloned() + }).min().unwrap(); + eprintln!("Part two: {min}"); + + Ok(()) +} + +fn find_starts((map_w, map_h): (usize, usize), input: &Input) -> Vec { + let mut starts = vec![]; + for x in 0..map_w { + for y in 0..map_h { + if matches!(input[y][x], 'a' | 'S') { + starts.push(Pos(x, y)); + } + } + } + starts +} + +#[derive(Default)] +struct Pos(usize, usize); + +impl Debug for Pos { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{},{}", self.0, self.1) + } +} + +impl Into for Pos { + fn into(self) -> NodeIndex { + NodeIndex::new(self.0 * 1000 + self.1) + } +} + +fn build_graph((map_w, map_h): (usize, usize), input: &Input) -> (DiGraph, ((usize,usize), (usize,usize))) { + let mut edges = vec![]; + + let mut start = None; + let mut end = None; + for x in 0..map_w { + for y in 0..map_h { + let h = input[y][x]; + + match h { + 'S' => start = Some((x, y)), + 'E' => end = Some((x, y)), + _ => {}, + } + + if x > 0 { + let (nx, ny) = (x - 1, y); + let h2 = input[ny][nx]; + let h = if h == 'S' { 'a' } else if h == 'E' { 'z' } else { h }; + let h2 = if h2 == 'S' { 'a' } else if h2 == 'E' { 'z' } else { h2 }; + let h = (h as usize) - ('a' as usize); + let h2 = (h2 as usize) - ('a' as usize); + if h >= h2 || h2 - h == 1 { + edges.push((Pos(x, y), Pos(nx, ny))); + } + } + + if y > 0 { + let (nx, ny) = (x, y - 1); + let h2 = input[ny][nx]; + let h = if h == 'S' { 'a' } else if h == 'E' { 'z' } else { h }; + let h2 = if h2 == 'S' { 'a' } else if h2 == 'E' { 'z' } else { h2 }; + let h = (h as usize) - ('a' as usize); + let h2 = (h2 as usize) - ('a' as usize); + if h >= h2 || h2 - h == 1 { + edges.push((Pos(x, y), Pos(nx, ny))); + } + } + + if x < map_w - 1 { + let (nx, ny) = (x + 1, y); + let h2 = input[ny][nx]; + let h = if h == 'S' { 'a' } else if h == 'E' { 'z' } else { h }; + let h2 = if h2 == 'S' { 'a' } else if h2 == 'E' { 'z' } else { h2 }; + let h = (h as usize) - ('a' as usize); + let h2 = (h2 as usize) - ('a' as usize); + if h >= h2 || h2 - h == 1 { + edges.push((Pos(x, y), Pos(nx, ny))); + } + } + + if y < map_h - 1 { + let (nx, ny) = (x, y + 1); + let h2 = input[ny][nx]; + let h = if h == 'S' { 'a' } else if h == 'E' { 'z' } else { h }; + let h2 = if h2 == 'S' { 'a' } else if h2 == 'E' { 'z' } else { h2 }; + let h = (h as usize) - ('a' as usize); + let h2 = (h2 as usize) - ('a' as usize); + if h >= h2 || h2 - h == 1 { + edges.push((Pos(x, y), Pos(nx, ny))); + } + } + } + } + + eprintln!("{}", edges.len()); + + let start = start.unwrap(); + let end = end.unwrap(); + + (DiGraph::from_edges(edges), (start, end)) +} diff --git a/test_cases/day_12.txt b/test_cases/day_12.txt new file mode 100644 index 0000000..433e0d2 --- /dev/null +++ b/test_cases/day_12.txt @@ -0,0 +1,5 @@ +Sabqponm +abcryxxl +accszExk +acctuvwj +abdefghi \ No newline at end of file