add day 13

This commit is contained in:
timeshifter
2026-06-23 18:05:36 +02:00
parent 0c75b1e03c
commit 6d39fd6411
4 changed files with 171 additions and 0 deletions
+83
View File
@@ -0,0 +1,83 @@
use std::collections::HashSet;
use itertools::Itertools;
fn main() {
let mut data: Vec<_> = std::fs::read_to_string("input.txt")
.unwrap()
.lines()
.map(Neighbors::parse)
.collect();
part1(&data);
let persons: HashSet<_> = data.iter().map(|p| p.persons.0.clone()).collect();
for p in &persons {
data.push(Neighbors::new(("me".to_string(), p.to_string()), 0));
data.push(Neighbors::new((p.to_string(), "me".to_string()), 0));
}
part1(&data);
}
fn part1(data: &[Neighbors]) {
let persons: HashSet<_> = data.iter().map(|p| p.persons.0.clone()).collect();
let result = persons
.iter()
.permutations(persons.len())
.map(|order| calculate_happiness(&order, data))
.max()
.unwrap();
println!("{result}");
}
fn calculate_happiness(order: &[&String], neighbors: &[Neighbors]) -> i64 {
let mut result = 0;
let mut order_wrapped = order.to_vec();
order_wrapped.push(order[0]);
for (a, b) in order_wrapped.iter().tuple_windows() {
result += neighbors
.iter()
.filter(|n| {
n.persons == (a.to_string(), b.to_string())
|| n.persons == (b.to_string(), a.to_string())
})
.map(|p| p.happiness)
.sum::<i64>();
}
result
}
#[derive(Debug)]
struct Neighbors {
pub persons: (String, String),
pub happiness: i64,
}
impl Neighbors {
fn new(persons: (String, String), happiness: i64) -> Self {
Self { persons, happiness }
}
fn parse(s: &str) -> Self {
let mut parts = s.split_whitespace();
let a = parts.next().unwrap().to_string();
let mut b = parts.next_back().unwrap().to_string();
b = b.chars().take(b.len() - 1).collect();
let lose_gain = parts.nth(1).unwrap();
let sign = if lose_gain == "gain" { 1 } else { -1 };
let amount: i64 = parts.next().unwrap().parse().unwrap();
let happiness = amount * sign;
Self::new((a, b), happiness)
}
}