From 317f9b5be06f6579bd3a4bb9fb4e6f5a5fe79522 Mon Sep 17 00:00:00 2001 From: Burnus Date: Mon, 24 Apr 2023 22:15:40 +0200 Subject: [PATCH] Added Solution for 2021 day 12 --- 2021/day12_passage_pathing/Cargo.toml | 8 + 2021/day12_passage_pathing/challenge.txt | 177 ++++++++++++++++++ 2021/day12_passage_pathing/src/lib.rs | 117 ++++++++++++ .../tests/challenge_input | 19 ++ 2021/day12_passage_pathing/tests/sample_input | 18 ++ 5 files changed, 339 insertions(+) create mode 100644 2021/day12_passage_pathing/Cargo.toml create mode 100644 2021/day12_passage_pathing/challenge.txt create mode 100644 2021/day12_passage_pathing/src/lib.rs create mode 100644 2021/day12_passage_pathing/tests/challenge_input create mode 100644 2021/day12_passage_pathing/tests/sample_input diff --git a/2021/day12_passage_pathing/Cargo.toml b/2021/day12_passage_pathing/Cargo.toml new file mode 100644 index 0000000..e7f32fc --- /dev/null +++ b/2021/day12_passage_pathing/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day12_passage_pathing" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/2021/day12_passage_pathing/challenge.txt b/2021/day12_passage_pathing/challenge.txt new file mode 100644 index 0000000..c542549 --- /dev/null +++ b/2021/day12_passage_pathing/challenge.txt @@ -0,0 +1,177 @@ +With your submarine's subterranean subsystems subsisting suboptimally, the only way you're getting out of this cave anytime soon is by finding a path yourself. Not just *a* path - the only way to know if you've found the *best* path is to find *all* of them. + +Fortunately, the sensors are still mostly working, and so you build a rough map of the remaining caves (your puzzle input). For example: + +``` +start-A +start-b +A-c +A-b +b-d +A-end +b-end + +``` + +This is a list of how all of the caves are connected. You start in the cave named `start`, and your destination is the cave named `end`. An entry like `b-d` means that cave `b` is connected to cave `d` - that is, you can move between them. + +So, the above cave system looks roughly like this: + +``` + start + / \ +c--A-----b--d + \ / + end + +``` + +Your goal is to find the number of distinct *paths* that start at `start`, end at `end`, and don't visit small caves more than once. There are two types of caves: *big* caves (written in uppercase, like `A`) and *small* caves (written in lowercase, like `b`). It would be a waste of time to visit any small cave more than once, but big caves are large enough that it might be worth visiting them multiple times. So, all paths you find should *visit small caves at most once*, and can *visit big caves any number of times*. + +Given these rules, there are `*10*` paths through this example cave system: + +``` +start,A,b,A,c,A,end +start,A,b,A,end +start,A,b,end +start,A,c,A,b,A,end +start,A,c,A,b,end +start,A,c,A,end +start,A,end +start,b,A,c,A,end +start,b,A,end +start,b,end + +``` + +(Each line in the above list corresponds to a single path; the caves visited by that path are listed in the order they are visited and separated by commas.) + +Note that in this cave system, cave `d` is never visited by any path: to do so, cave `b` would need to be visited twice (once on the way to cave `d` and a second time when returning from cave `d`), and since cave `b` is small, this is not allowed. + +Here is a slightly larger example: + +``` +dc-end +HN-start +start-kj +dc-start +dc-HN +LN-dc +HN-end +kj-sa +kj-HN +kj-dc + +``` + +The `19` paths through it are as follows: + +``` +start,HN,dc,HN,end +start,HN,dc,HN,kj,HN,end +start,HN,dc,end +start,HN,dc,kj,HN,end +start,HN,end +start,HN,kj,HN,dc,HN,end +start,HN,kj,HN,dc,end +start,HN,kj,HN,end +start,HN,kj,dc,HN,end +start,HN,kj,dc,end +start,dc,HN,end +start,dc,HN,kj,HN,end +start,dc,end +start,dc,kj,HN,end +start,kj,HN,dc,HN,end +start,kj,HN,dc,end +start,kj,HN,end +start,kj,dc,HN,end +start,kj,dc,end + +``` + +Finally, this even larger example has `226` paths through it: + +``` +fs-end +he-DX +fs-he +start-DX +pj-DX +end-zg +zg-sl +zg-pj +pj-he +RW-he +fs-DX +pj-RW +zg-RW +start-pj +he-WI +zg-he +pj-fs +start-RW + +``` + +*How many paths through this cave system are there that visit small caves at most once?* + +Your puzzle answer was `3708`. + +\--- Part Two --- +---------- + +After reviewing the available paths, you realize you might have time to visit a single small cave *twice*. Specifically, big caves can be visited any number of times, a single small cave can be visited at most twice, and the remaining small caves can be visited at most once. However, the caves named `start` and `end` can only be visited *exactly once each*: once you leave the `start` cave, you may not return to it, and once you reach the `end` cave, the path must end immediately. + +Now, the `36` possible paths through the first example above are: + +``` +start,A,b,A,b,A,c,A,end +start,A,b,A,b,A,end +start,A,b,A,b,end +start,A,b,A,c,A,b,A,end +start,A,b,A,c,A,b,end +start,A,b,A,c,A,c,A,end +start,A,b,A,c,A,end +start,A,b,A,end +start,A,b,d,b,A,c,A,end +start,A,b,d,b,A,end +start,A,b,d,b,end +start,A,b,end +start,A,c,A,b,A,b,A,end +start,A,c,A,b,A,b,end +start,A,c,A,b,A,c,A,end +start,A,c,A,b,A,end +start,A,c,A,b,d,b,A,end +start,A,c,A,b,d,b,end +start,A,c,A,b,end +start,A,c,A,c,A,b,A,end +start,A,c,A,c,A,b,end +start,A,c,A,c,A,end +start,A,c,A,end +start,A,end +start,b,A,b,A,c,A,end +start,b,A,b,A,end +start,b,A,b,end +start,b,A,c,A,b,A,end +start,b,A,c,A,b,end +start,b,A,c,A,c,A,end +start,b,A,c,A,end +start,b,A,end +start,b,d,b,A,c,A,end +start,b,d,b,A,end +start,b,d,b,end +start,b,end + +``` + +The slightly larger example above now has `103` paths through it, and the even larger example now has `3509` paths through it. + +Given these new rules, *how many paths through this cave system are there?* + +Your puzzle answer was `93858`. + +Both parts of this puzzle are complete! They provide two gold stars: \*\* + +At this point, you should [return to your Advent calendar](/2021) and try another puzzle. + +If you still want to see it, you can [get your puzzle input](12/input). \ No newline at end of file diff --git a/2021/day12_passage_pathing/src/lib.rs b/2021/day12_passage_pathing/src/lib.rs new file mode 100644 index 0000000..71dc74c --- /dev/null +++ b/2021/day12_passage_pathing/src/lib.rs @@ -0,0 +1,117 @@ +use core::fmt::Display; + +#[derive(Debug, PartialEq, Eq)] +pub enum ParseError { + LineMalformed(String), +} + +impl Display for ParseError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::LineMalformed(v) => write!(f, "Line is malformed: {v}"), + } + } +} + +#[derive(Default)] +struct Cavern { + is_large: bool, + neighbours: Vec, +} + +struct Network { + caverns: Vec, +} + +impl TryFrom<&str> for Network { + type Error = ParseError; + + fn try_from(value: &str) -> Result { + let mut caverns = vec![Cavern::default(), Cavern::default()]; + let mut names = vec!["start", "end"]; + + for line in value.lines() { + if let Some((lhs, rhs)) = line.split_once('-') { + let lhs = match names.iter().position(|name| *name == lhs) { + Some(idx) => idx, + None => { + let is_large = lhs.chars().next().unwrap().is_uppercase(); + names.push(lhs); + caverns.push(Cavern { is_large, ..Default::default() }); + caverns.len()-1 + } + }; + let rhs = match names.iter().position(|name| *name == rhs) { + Some(idx) => idx, + None => { + let is_large = rhs.chars().next().unwrap().is_uppercase(); + names.push(rhs); + caverns.push(Cavern { is_large, ..Default::default() }); + caverns.len()-1 + } + }; + caverns[lhs].neighbours.push(rhs); + caverns[rhs].neighbours.push(lhs); + } else { + return Err(Self::Error::LineMalformed(line.to_string())); + } + } + Ok(Self { caverns }) + } +} + +impl Network { + fn get_paths(&self, revisits: usize) -> Vec> { + let mut open_set = vec![(vec![0], revisits)]; + let mut res = Vec::new(); + + while let Some(path) = open_set.pop() { + let current = path.0[path.0.len()-1]; + if current == 1 { + res.push(path.0); + } else { + for neighbour in &self.caverns[current].neighbours { + if self.caverns[*neighbour].is_large || !path.0.contains(neighbour) { + let mut new_path = path.0.clone(); + new_path.push(*neighbour); + open_set.push((new_path, path.1)); + } else if path.1 > 0 && *neighbour > 0 { + let mut new_path = path.0.clone(); + new_path.push(*neighbour); + open_set.push((new_path, path.1-1)); + } + } + } + } + res + } +} + +pub fn run(input: &str) -> Result<(usize, usize), ParseError> { + let network = Network::try_from(input)?; + let first = network.get_paths(0).len(); + let second = network.get_paths(1).len(); + Ok((first, second)) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::fs::read_to_string; + + fn read_file(name: &str) -> String { + read_to_string(name).expect(&format!("Unable to read file: {name}")[..]).trim().to_string() + } + + #[test] + fn test_sample() { + let sample_input = read_file("tests/sample_input"); + assert_eq!(run(&sample_input), Ok((226, 3509))); + } + + #[test] + fn test_challenge() { + let challenge_input = read_file("tests/challenge_input"); + assert_eq!(run(&challenge_input), Ok((3708, 93858))); + } +} diff --git a/2021/day12_passage_pathing/tests/challenge_input b/2021/day12_passage_pathing/tests/challenge_input new file mode 100644 index 0000000..262ee68 --- /dev/null +++ b/2021/day12_passage_pathing/tests/challenge_input @@ -0,0 +1,19 @@ +lg-GW +pt-start +pt-uq +nx-lg +ve-GW +start-nx +GW-start +GW-nx +pt-SM +sx-GW +lg-end +nx-SM +lg-SM +pt-nx +end-ve +ve-SM +TG-uq +end-SM +SM-uq diff --git a/2021/day12_passage_pathing/tests/sample_input b/2021/day12_passage_pathing/tests/sample_input new file mode 100644 index 0000000..65f3833 --- /dev/null +++ b/2021/day12_passage_pathing/tests/sample_input @@ -0,0 +1,18 @@ +fs-end +he-DX +fs-he +start-DX +pj-DX +end-zg +zg-sl +zg-pj +pj-he +RW-he +fs-DX +pj-RW +zg-RW +start-pj +he-WI +zg-he +pj-fs +start-RW