From 6d39fd641189b78277eaaa5ba4c3cbe5f2f1f51a Mon Sep 17 00:00:00 2001 From: timeshifter Date: Tue, 23 Jun 2026 18:05:36 +0200 Subject: [PATCH] add day 13 --- day13/Cargo.lock | 25 ++++++++++++++ day13/Cargo.toml | 7 ++++ day13/input.txt | 56 ++++++++++++++++++++++++++++++++ day13/src/main.rs | 83 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 171 insertions(+) create mode 100644 day13/Cargo.lock create mode 100644 day13/Cargo.toml create mode 100644 day13/input.txt create mode 100644 day13/src/main.rs diff --git a/day13/Cargo.lock b/day13/Cargo.lock new file mode 100644 index 0000000..3415aa7 --- /dev/null +++ b/day13/Cargo.lock @@ -0,0 +1,25 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "day13" +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", +] diff --git a/day13/Cargo.toml b/day13/Cargo.toml new file mode 100644 index 0000000..93889a6 --- /dev/null +++ b/day13/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "day13" +version = "0.1.0" +edition = "2024" + +[dependencies] +itertools = "*" diff --git a/day13/input.txt b/day13/input.txt new file mode 100644 index 0000000..9ddfd38 --- /dev/null +++ b/day13/input.txt @@ -0,0 +1,56 @@ +Alice would lose 2 happiness units by sitting next to Bob. +Alice would lose 62 happiness units by sitting next to Carol. +Alice would gain 65 happiness units by sitting next to David. +Alice would gain 21 happiness units by sitting next to Eric. +Alice would lose 81 happiness units by sitting next to Frank. +Alice would lose 4 happiness units by sitting next to George. +Alice would lose 80 happiness units by sitting next to Mallory. +Bob would gain 93 happiness units by sitting next to Alice. +Bob would gain 19 happiness units by sitting next to Carol. +Bob would gain 5 happiness units by sitting next to David. +Bob would gain 49 happiness units by sitting next to Eric. +Bob would gain 68 happiness units by sitting next to Frank. +Bob would gain 23 happiness units by sitting next to George. +Bob would gain 29 happiness units by sitting next to Mallory. +Carol would lose 54 happiness units by sitting next to Alice. +Carol would lose 70 happiness units by sitting next to Bob. +Carol would lose 37 happiness units by sitting next to David. +Carol would lose 46 happiness units by sitting next to Eric. +Carol would gain 33 happiness units by sitting next to Frank. +Carol would lose 35 happiness units by sitting next to George. +Carol would gain 10 happiness units by sitting next to Mallory. +David would gain 43 happiness units by sitting next to Alice. +David would lose 96 happiness units by sitting next to Bob. +David would lose 53 happiness units by sitting next to Carol. +David would lose 30 happiness units by sitting next to Eric. +David would lose 12 happiness units by sitting next to Frank. +David would gain 75 happiness units by sitting next to George. +David would lose 20 happiness units by sitting next to Mallory. +Eric would gain 8 happiness units by sitting next to Alice. +Eric would lose 89 happiness units by sitting next to Bob. +Eric would lose 69 happiness units by sitting next to Carol. +Eric would lose 34 happiness units by sitting next to David. +Eric would gain 95 happiness units by sitting next to Frank. +Eric would gain 34 happiness units by sitting next to George. +Eric would lose 99 happiness units by sitting next to Mallory. +Frank would lose 97 happiness units by sitting next to Alice. +Frank would gain 6 happiness units by sitting next to Bob. +Frank would lose 9 happiness units by sitting next to Carol. +Frank would gain 56 happiness units by sitting next to David. +Frank would lose 17 happiness units by sitting next to Eric. +Frank would gain 18 happiness units by sitting next to George. +Frank would lose 56 happiness units by sitting next to Mallory. +George would gain 45 happiness units by sitting next to Alice. +George would gain 76 happiness units by sitting next to Bob. +George would gain 63 happiness units by sitting next to Carol. +George would gain 54 happiness units by sitting next to David. +George would gain 54 happiness units by sitting next to Eric. +George would gain 30 happiness units by sitting next to Frank. +George would gain 7 happiness units by sitting next to Mallory. +Mallory would gain 31 happiness units by sitting next to Alice. +Mallory would lose 32 happiness units by sitting next to Bob. +Mallory would gain 95 happiness units by sitting next to Carol. +Mallory would gain 91 happiness units by sitting next to David. +Mallory would lose 66 happiness units by sitting next to Eric. +Mallory would lose 75 happiness units by sitting next to Frank. +Mallory would lose 99 happiness units by sitting next to George. diff --git a/day13/src/main.rs b/day13/src/main.rs new file mode 100644 index 0000000..67ffe99 --- /dev/null +++ b/day13/src/main.rs @@ -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::(); + } + + 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) + } +}