add day 8 through 10
This commit is contained in:
Generated
+25
@@ -0,0 +1,25 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "day09"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"itertools",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.16.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "91622ff5e7162018101f2fea40d6ebf4a78bbe5a49736a2020649edf9693679e"
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.15.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8b4baf93f58d4425749ca49a51c50ebab072c5df6994d08fed93541c331481dc"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
||||
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "day09"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
itertools = "0.15.0"
|
||||
@@ -0,0 +1,28 @@
|
||||
AlphaCentauri to Snowdin = 66
|
||||
AlphaCentauri to Tambi = 28
|
||||
AlphaCentauri to Faerun = 60
|
||||
AlphaCentauri to Norrath = 34
|
||||
AlphaCentauri to Straylight = 34
|
||||
AlphaCentauri to Tristram = 3
|
||||
AlphaCentauri to Arbre = 108
|
||||
Snowdin to Tambi = 22
|
||||
Snowdin to Faerun = 12
|
||||
Snowdin to Norrath = 91
|
||||
Snowdin to Straylight = 121
|
||||
Snowdin to Tristram = 111
|
||||
Snowdin to Arbre = 71
|
||||
Tambi to Faerun = 39
|
||||
Tambi to Norrath = 113
|
||||
Tambi to Straylight = 130
|
||||
Tambi to Tristram = 35
|
||||
Tambi to Arbre = 40
|
||||
Faerun to Norrath = 63
|
||||
Faerun to Straylight = 21
|
||||
Faerun to Tristram = 57
|
||||
Faerun to Arbre = 83
|
||||
Norrath to Straylight = 9
|
||||
Norrath to Tristram = 50
|
||||
Norrath to Arbre = 60
|
||||
Straylight to Tristram = 27
|
||||
Straylight to Arbre = 81
|
||||
Tristram to Arbre = 90
|
||||
@@ -0,0 +1,68 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
use itertools::Itertools;
|
||||
|
||||
fn main() {
|
||||
let data = std::fs::read_to_string("input.txt").unwrap();
|
||||
let legs: Vec<_> = data.lines().map(Leg::from_str).collect();
|
||||
|
||||
part1(&legs);
|
||||
}
|
||||
|
||||
fn part1(legs: &[Leg]) {
|
||||
let mut locations = HashSet::new();
|
||||
for (s, e) in legs.iter().map(|l| (l.from.as_str(), l.to.as_str())) {
|
||||
locations.insert(s);
|
||||
locations.insert(e);
|
||||
}
|
||||
|
||||
let length = locations.len();
|
||||
|
||||
let routes = locations.into_iter().permutations(length).collect_vec();
|
||||
|
||||
let mut sums = vec![];
|
||||
|
||||
for route in routes {
|
||||
let sum: usize = route
|
||||
.iter()
|
||||
.tuple_windows()
|
||||
.map(|(l, r)| find_length(legs, l, r))
|
||||
.sum();
|
||||
sums.push(sum);
|
||||
}
|
||||
sums.sort_unstable();
|
||||
println!("{}\n{}", sums[0], sums.last().unwrap());
|
||||
}
|
||||
|
||||
fn find_length(legs: &[Leg], a: &str, b: &str) -> usize {
|
||||
for leg in legs {
|
||||
if (leg.from == a || leg.to == a) && (leg.from == b || leg.to == b) {
|
||||
return leg.distance;
|
||||
}
|
||||
}
|
||||
unreachable!()
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Leg {
|
||||
pub from: String,
|
||||
pub to: String,
|
||||
pub distance: usize,
|
||||
}
|
||||
|
||||
impl Leg {
|
||||
fn new(from: String, to: String, distance: usize) -> Self {
|
||||
Self { from, to, distance }
|
||||
}
|
||||
|
||||
fn from_str(s: &str) -> Self {
|
||||
let mut segments = s.split_whitespace();
|
||||
let from = segments.next().unwrap().to_string();
|
||||
let to = segments.nth(1).unwrap().to_string();
|
||||
let distance = segments.next_back().unwrap().parse().unwrap();
|
||||
|
||||
Self::new(from, to, distance)
|
||||
}
|
||||
}
|
||||
|
||||
// https://adventofcode.com/2015/day/9
|
||||
Reference in New Issue
Block a user