add day 13
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 = "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",
|
||||
]
|
||||
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "day13"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
itertools = "*"
|
||||
@@ -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.
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user