2015/08-10, 2015/12-13, started on 2015/14
This commit is contained in:
4
2015/13/Cargo.lock
generated
Normal file
4
2015/13/Cargo.lock
generated
Normal file
@@ -0,0 +1,4 @@
|
||||
[[package]]
|
||||
name = "day-13"
|
||||
version = "0.1.0"
|
||||
|
||||
6
2015/13/Cargo.toml
Normal file
6
2015/13/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "day-13"
|
||||
version = "0.1.0"
|
||||
authors = ["logaritmisk <anders.e.olsson@gmail.com>"]
|
||||
|
||||
[dependencies]
|
||||
12
2015/13/example.txt
Normal file
12
2015/13/example.txt
Normal file
@@ -0,0 +1,12 @@
|
||||
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.
|
||||
56
2015/13/input.txt
Normal file
56
2015/13/input.txt
Normal file
@@ -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.
|
||||
254
2015/13/src/main.rs
Normal file
254
2015/13/src/main.rs
Normal file
@@ -0,0 +1,254 @@
|
||||
use std::io::{self, Read};
|
||||
use std::iter::Iterator;
|
||||
use std::collections::HashMap;
|
||||
|
||||
|
||||
type Map<'a> = HashMap<&'a str, HashMap<&'a str, i64>>;
|
||||
|
||||
|
||||
struct Permutations<'a, T: 'a> {
|
||||
source: &'a [T],
|
||||
iter: Vec<usize>,
|
||||
done: bool,
|
||||
}
|
||||
|
||||
impl<'a, T> Permutations<'a, T> {
|
||||
fn new(source: &'a [T]) -> Permutations<T> {
|
||||
Permutations {
|
||||
source: source,
|
||||
iter: vec![0; source.len()],
|
||||
done: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> Iterator for Permutations<'a, T>
|
||||
where
|
||||
T: Clone,
|
||||
{
|
||||
type Item = Vec<T>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.done {
|
||||
None
|
||||
} else {
|
||||
let values = self.iter
|
||||
.iter()
|
||||
.map(|index| self.source[*index].clone())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let max = self.source.len() - 1;
|
||||
|
||||
self.done = true;
|
||||
|
||||
for i in self.iter.iter_mut().rev() {
|
||||
if *i == max {
|
||||
*i = 0;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
*i += 1;
|
||||
self.done = false;
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
Some(values)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
enum State {
|
||||
From,
|
||||
TriggerTo,
|
||||
To,
|
||||
Sign,
|
||||
Happiness(bool),
|
||||
}
|
||||
|
||||
fn parse(input: &str) -> (&str, &str, i64) {
|
||||
let mut state = State::From;
|
||||
|
||||
let mut from = None;
|
||||
let mut to = None;
|
||||
let mut happiness = None;
|
||||
|
||||
for token in input.split_whitespace() {
|
||||
match (state, token) {
|
||||
(State::From, name) => {
|
||||
from = Some(name);
|
||||
|
||||
state = State::Sign;
|
||||
}
|
||||
|
||||
(State::Sign, "gain") => {
|
||||
state = State::Happiness(true);
|
||||
}
|
||||
(State::Sign, "lose") => {
|
||||
state = State::Happiness(false);
|
||||
}
|
||||
|
||||
(State::Happiness(sign), value) => {
|
||||
happiness = Some(value.parse::<i64>().unwrap() * if sign { 1 } else { -1 });
|
||||
|
||||
state = State::TriggerTo;
|
||||
}
|
||||
|
||||
(State::TriggerTo, "to") => {
|
||||
state = State::To;
|
||||
}
|
||||
|
||||
(State::To, name) => {
|
||||
let len = name.len();
|
||||
|
||||
to = Some(&name[0..len - 1]);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
(_, _) => (),
|
||||
}
|
||||
}
|
||||
|
||||
(from.unwrap(), to.unwrap(), happiness.unwrap())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut input = String::new();
|
||||
|
||||
io::stdin()
|
||||
.read_to_string(&mut input)
|
||||
.expect("failed to read input");
|
||||
|
||||
let mut map = Map::new();
|
||||
|
||||
for line in input.lines().filter(|line| !line.is_empty()) {
|
||||
let (from, to, happines) = parse(line);
|
||||
|
||||
map.entry(from)
|
||||
.or_insert_with(HashMap::new)
|
||||
.insert(to, happines);
|
||||
}
|
||||
|
||||
{
|
||||
let values = map.keys().collect::<Vec<_>>();
|
||||
|
||||
let mut permutations = Permutations::new(&values)
|
||||
.filter(|permutation| {
|
||||
let mut iter = permutation.iter();
|
||||
|
||||
while let Some(needle) = iter.next() {
|
||||
if iter.clone().any(|haystack| haystack == needle) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
})
|
||||
.map(|permutation| {
|
||||
let mut table = permutation.clone();
|
||||
|
||||
table.push(permutation.first().unwrap());
|
||||
|
||||
let happiness: i64 = table
|
||||
.windows(2)
|
||||
.map(|people| {
|
||||
map[people[0]][people[1]] + map[people[1]][people[0]]
|
||||
})
|
||||
.sum();
|
||||
|
||||
(permutation, happiness)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
permutations.sort_by_key(|&(_, happiness)| happiness);
|
||||
|
||||
println!("part_one={:?}", permutations.iter().rev().next().unwrap());
|
||||
}
|
||||
|
||||
for other in map.values_mut() {
|
||||
other.insert("Me", 0);
|
||||
}
|
||||
|
||||
let others = map.keys().map(|other| *other).collect::<Vec<_>>();
|
||||
|
||||
for other in others {
|
||||
map.entry("Me")
|
||||
.or_insert_with(HashMap::new)
|
||||
.insert(other, 0);
|
||||
}
|
||||
|
||||
{
|
||||
let values = map.keys().collect::<Vec<_>>();
|
||||
|
||||
let mut permutations = Permutations::new(&values)
|
||||
.filter(|permutation| {
|
||||
let mut iter = permutation.iter();
|
||||
|
||||
while let Some(needle) = iter.next() {
|
||||
if iter.clone().any(|haystack| haystack == needle) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
})
|
||||
.map(|permutation| {
|
||||
let mut table = permutation.clone();
|
||||
|
||||
table.push(permutation.first().unwrap());
|
||||
|
||||
let happiness: i64 = table
|
||||
.windows(2)
|
||||
.map(|people| {
|
||||
map[people[0]][people[1]] + map[people[1]][people[0]]
|
||||
})
|
||||
.sum();
|
||||
|
||||
(permutation, happiness)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
permutations.sort_by_key(|&(_, happiness)| happiness);
|
||||
|
||||
println!("part_two={:?}", permutations.iter().rev().next().unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_parse() {
|
||||
assert_eq!(
|
||||
parse("Alice would gain 54 happiness units by sitting next to Bob."),
|
||||
("Alice", "Bob", 54)
|
||||
);
|
||||
assert_eq!(
|
||||
parse("Alice would lose 79 happiness units by sitting next to Carol."),
|
||||
("Alice", "Carol", -79)
|
||||
);
|
||||
assert_eq!(
|
||||
parse("Alice would lose 2 happiness units by sitting next to David."),
|
||||
("Alice", "David", -2)
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
parse("Bob would gain 83 happiness units by sitting next to Alice."),
|
||||
("Bob", "Alice", 83)
|
||||
);
|
||||
assert_eq!(
|
||||
parse("Bob would lose 7 happiness units by sitting next to Carol."),
|
||||
("Bob", "Carol", -7)
|
||||
);
|
||||
assert_eq!(
|
||||
parse("Bob would lose 63 happiness units by sitting next to David."),
|
||||
("Bob", "David", -63)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user