add day 8 through 10

This commit is contained in:
timeshifter
2026-06-21 21:44:46 +02:00
parent 037c0d649c
commit 9adb5887bb
11 changed files with 647 additions and 0 deletions
+68
View File
@@ -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