Day 13 part 1.
This commit is contained in:
82
src/day13.rs
Normal file
82
src/day13.rs
Normal file
@@ -0,0 +1,82 @@
|
||||
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 left = if i == 0 { arrangement.len() - 1 } else { i - 1 };
|
||||
let right = (i + 1) % arrangement.len();
|
||||
happiness += deltas[&(arrangement[left].clone(), arrangement[i].clone())] +
|
||||
deltas[&(arrangement[i].clone(), arrangement[left].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(input: &str) -> String {
|
||||
// todo!()
|
||||
// }
|
||||
|
||||
#[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);
|
||||
}
|
||||
}
|
||||
@@ -5,5 +5,6 @@ extern crate aoc_runner_derive;
|
||||
mod day10;
|
||||
mod day11;
|
||||
mod day12;
|
||||
mod day13;
|
||||
|
||||
aoc_lib! { year = 2015 }
|
||||
|
||||
Reference in New Issue
Block a user