summaryrefslogtreecommitdiffstats
path: root/2015/rs/code/nine
diff options
context:
space:
mode:
authoralyx <alyx@aleteoryx.me>2023-11-20 17:12:15 -0500
committeralyx <alyx@aleteoryx.me>2023-11-20 17:12:15 -0500
commitccf1a5828fc26a82545c7accf1ce7916daa08a2d (patch)
tree2428c1374e8f9bcbfd00c8090606f4781d083401 /2015/rs/code/nine
parent4810e24657a5931ea79be3fc3ee3e108fe40282e (diff)
downloadadventofcode-ccf1a5828fc26a82545c7accf1ce7916daa08a2d.tar.gz
adventofcode-ccf1a5828fc26a82545c7accf1ce7916daa08a2d.tar.bz2
adventofcode-ccf1a5828fc26a82545c7accf1ce7916daa08a2d.zip
Reorganize using workspaces
Diffstat (limited to '2015/rs/code/nine')
-rw-r--r--2015/rs/code/nine/Cargo.toml8
-rw-r--r--2015/rs/code/nine/src/input.txt28
-rw-r--r--2015/rs/code/nine/src/main.rs53
3 files changed, 89 insertions, 0 deletions
diff --git a/2015/rs/code/nine/Cargo.toml b/2015/rs/code/nine/Cargo.toml
new file mode 100644
index 0000000..4782be7
--- /dev/null
+++ b/2015/rs/code/nine/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "nine"
+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/2015/rs/code/nine/src/input.txt b/2015/rs/code/nine/src/input.txt
new file mode 100644
index 0000000..97a6b63
--- /dev/null
+++ b/2015/rs/code/nine/src/input.txt
@@ -0,0 +1,28 @@
+Faerun to Tristram = 65
+Faerun to Tambi = 129
+Faerun to Norrath = 144
+Faerun to Snowdin = 71
+Faerun to Straylight = 137
+Faerun to AlphaCentauri = 3
+Faerun to Arbre = 149
+Tristram to Tambi = 63
+Tristram to Norrath = 4
+Tristram to Snowdin = 105
+Tristram to Straylight = 125
+Tristram to AlphaCentauri = 55
+Tristram to Arbre = 14
+Tambi to Norrath = 68
+Tambi to Snowdin = 52
+Tambi to Straylight = 65
+Tambi to AlphaCentauri = 22
+Tambi to Arbre = 143
+Norrath to Snowdin = 8
+Norrath to Straylight = 23
+Norrath to AlphaCentauri = 136
+Norrath to Arbre = 115
+Snowdin to Straylight = 101
+Snowdin to AlphaCentauri = 84
+Snowdin to Arbre = 96
+Straylight to AlphaCentauri = 107
+Straylight to Arbre = 14
+AlphaCentauri to Arbre = 46
diff --git a/2015/rs/code/nine/src/main.rs b/2015/rs/code/nine/src/main.rs
new file mode 100644
index 0000000..46116e0
--- /dev/null
+++ b/2015/rs/code/nine/src/main.rs
@@ -0,0 +1,53 @@
+#![feature(iter_next_chunk)]
+
+use std::collections::{HashSet, HashMap};
+use std::io::Write;
+
+static INPUT: &'static str = include_str!("input.txt");
+
+fn main() {
+ let valueified = INPUT.split('\n').filter(|s| !s.is_empty())
+ .flat_map(|s| {
+ let [rhs, dist] = s.split(" = ").next_chunk::<2>().unwrap();
+ let [from, to] = rhs.split(" to ").next_chunk::<2>().unwrap();
+ let dist = dist.parse::<usize>().unwrap();
+ [(from, to, dist), (to, from, dist)]
+ });
+
+ let paths: HashMap<&'static str, Vec<(&'static str, usize)>> = valueified
+ .map(|(a,b,c)| (a, (b, c)))
+ .fold(HashMap::new(), |mut map, (k, v)| {map.entry(k).or_default().push(v); map});
+ let mut path = Vec::new();
+ let mut bestpath = Vec::new();
+ let mut bestdist = usize::MAX;
+ let mut worstpath = Vec::new();
+ let mut worstdist = 0;
+ for start in paths.keys() {
+ path.clear();
+ path.push(*start);
+ walk_steps(&mut path, 0, &mut bestpath, &mut bestdist, &mut worstpath, &mut worstdist, &paths);
+ }
+ println!("Best route: {} with dist = {bestdist}", bestpath.join(" => "));
+ println!("Worst route: {} with dist = {worstdist}", worstpath.join(" => "));
+}
+
+fn walk_steps(path: &mut Vec<&'static str>, dist: usize, bestpath: &mut Vec<&'static str>, bestdist: &mut usize, worstpath: &mut Vec<&'static str>, worstdist: &mut usize, steps: &HashMap<&'static str, Vec<(&'static str, usize)>>) {
+ if path.len() != steps.len() {
+ let prev = path.last().unwrap();
+ for (step, stepdist) in &steps[prev] {
+ if path.contains(step) { continue; }
+ path.push(step);
+ walk_steps(path, dist + stepdist, bestpath, bestdist, worstpath, worstdist, steps);
+ path.pop();
+ }
+ } else {
+ if dist < *bestdist {
+ *bestpath = path.clone();
+ *bestdist = dist;
+ }
+ else if dist > *worstdist {
+ *worstpath = path.clone();
+ *worstdist = dist;
+ }
+ }
+}