109 lines
3.4 KiB
Rust
109 lines
3.4 KiB
Rust
use std::collections::{HashMap, HashSet};
|
|
|
|
use aoc_runner_derive::{aoc, aoc_generator};
|
|
|
|
#[aoc_generator(day13)]
|
|
fn parse(input: &str) -> HashMap<(String, String), i32> {
|
|
input
|
|
.lines()
|
|
.map(|line| {
|
|
let v: Vec<&str> = line[0..line.len() - 1].split(" ").collect();
|
|
let from = v[0];
|
|
let to = v[10];
|
|
let mut value = v[3].parse::<i32>().unwrap();
|
|
if v[2] == "lose" {
|
|
value *= -1;
|
|
}
|
|
((from.into(), to.into()), value)
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
#[aoc(day13, part1)]
|
|
fn part1(deltas: &HashMap<(String, String), i32>) -> i32 {
|
|
let names: HashSet<_> = deltas.keys().map(|s| s.0.clone()).collect();
|
|
permutations(names)
|
|
.iter()
|
|
.map(|arrangement| {
|
|
let mut happiness = 0;
|
|
for i in 0..arrangement.len() {
|
|
let right = (i + 1) % arrangement.len();
|
|
happiness += deltas[&(arrangement[right].clone(), arrangement[i].clone())]
|
|
+ deltas[&(arrangement[i].clone(), arrangement[right].clone())];
|
|
}
|
|
happiness
|
|
})
|
|
.max()
|
|
.unwrap()
|
|
}
|
|
|
|
fn permutations(values: HashSet<String>) -> Vec<Vec<String>> {
|
|
let base: Vec<String> = values.into_iter().collect();
|
|
p(base)
|
|
}
|
|
|
|
fn p(mut input: Vec<String>) -> Vec<Vec<String>> {
|
|
if input.len() == 1 {
|
|
return vec![input];
|
|
}
|
|
let mut results = Vec::new();
|
|
for _ in 0..input.len() {
|
|
p(input.iter().skip(1).map(|t| t.clone()).collect())
|
|
.into_iter()
|
|
.for_each(|mut v| {
|
|
v.insert(0, input[0].clone());
|
|
results.push(v);
|
|
});
|
|
let v = input.remove(0);
|
|
input.push(v);
|
|
}
|
|
results
|
|
}
|
|
|
|
#[aoc(day13, part2)]
|
|
fn part2(deltas: &HashMap<(String, String), i32>) -> i32 {
|
|
let mut names: HashSet<_> = deltas.keys().map(|s| s.0.clone()).collect();
|
|
names.insert("me".to_string());
|
|
permutations(names)
|
|
.iter()
|
|
.map(|arrangement| {
|
|
let mut happiness = 0;
|
|
for i in 0..arrangement.len() {
|
|
let right = (i + 1) % arrangement.len();
|
|
happiness += deltas
|
|
.get(&(arrangement[right].clone(), arrangement[i].clone()))
|
|
.or(Some(&0))
|
|
.unwrap()
|
|
+ deltas
|
|
.get(&(arrangement[i].clone(), arrangement[right].clone()))
|
|
.or(Some(&0))
|
|
.unwrap();
|
|
}
|
|
happiness
|
|
})
|
|
.max()
|
|
.unwrap()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_part1() {
|
|
let input = "Alice would gain 54 happiness units by sitting next to Bob.
|
|
Alice would lose 79 happiness units by sitting next to Carol.
|
|
Alice would lose 2 happiness units by sitting next to David.
|
|
Bob would gain 83 happiness units by sitting next to Alice.
|
|
Bob would lose 7 happiness units by sitting next to Carol.
|
|
Bob would lose 63 happiness units by sitting next to David.
|
|
Carol would lose 62 happiness units by sitting next to Alice.
|
|
Carol would gain 60 happiness units by sitting next to Bob.
|
|
Carol would gain 55 happiness units by sitting next to David.
|
|
David would gain 46 happiness units by sitting next to Alice.
|
|
David would lose 7 happiness units by sitting next to Bob.
|
|
David would gain 41 happiness units by sitting next to Carol.";
|
|
assert_eq!(part1(&parse(input)), 330);
|
|
}
|
|
}
|