Skip to content

Commit 884dd14

Browse files
committed
Year 2015 Day 13
1 parent 6cde2bb commit 884dd14

File tree

8 files changed

+172
-0
lines changed

8 files changed

+172
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,4 @@ pie
248248
| 10 | [Elves Look, Elves Say](https://adventofcode.com/2015/day/10) | [Source](src/year2015/day10.rs) | 14 |
249249
| 11 | [Corporate Policy](https://adventofcode.com/2015/day/11) | [Source](src/year2015/day11.rs) | 1 |
250250
| 12 | [JSAbacusFramework.io](https://adventofcode.com/2015/day/12) | [Source](src/year2015/day12.rs) | 80 |
251+
| 13 | [Knights of the Dinner Table](https://adventofcode.com/2015/day/13) | [Source](src/year2015/day13.rs) | 39 |

benches/benchmark.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ mod year2015 {
4848
benchmark!(year2015, day10);
4949
benchmark!(year2015, day11);
5050
benchmark!(year2015, day12);
51+
benchmark!(year2015, day13);
5152
}
5253

5354
mod year2019 {

input/year2015/day13.txt

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
Alice would lose 57 happiness units by sitting next to Bob.
2+
Alice would lose 62 happiness units by sitting next to Carol.
3+
Alice would lose 75 happiness units by sitting next to David.
4+
Alice would gain 71 happiness units by sitting next to Eric.
5+
Alice would lose 22 happiness units by sitting next to Frank.
6+
Alice would lose 23 happiness units by sitting next to George.
7+
Alice would lose 76 happiness units by sitting next to Mallory.
8+
Bob would lose 14 happiness units by sitting next to Alice.
9+
Bob would gain 48 happiness units by sitting next to Carol.
10+
Bob would gain 89 happiness units by sitting next to David.
11+
Bob would gain 86 happiness units by sitting next to Eric.
12+
Bob would lose 2 happiness units by sitting next to Frank.
13+
Bob would gain 27 happiness units by sitting next to George.
14+
Bob would gain 19 happiness units by sitting next to Mallory.
15+
Carol would gain 37 happiness units by sitting next to Alice.
16+
Carol would gain 45 happiness units by sitting next to Bob.
17+
Carol would gain 24 happiness units by sitting next to David.
18+
Carol would gain 5 happiness units by sitting next to Eric.
19+
Carol would lose 68 happiness units by sitting next to Frank.
20+
Carol would lose 25 happiness units by sitting next to George.
21+
Carol would gain 30 happiness units by sitting next to Mallory.
22+
David would lose 51 happiness units by sitting next to Alice.
23+
David would gain 34 happiness units by sitting next to Bob.
24+
David would gain 99 happiness units by sitting next to Carol.
25+
David would gain 91 happiness units by sitting next to Eric.
26+
David would lose 38 happiness units by sitting next to Frank.
27+
David would gain 60 happiness units by sitting next to George.
28+
David would lose 63 happiness units by sitting next to Mallory.
29+
Eric would gain 23 happiness units by sitting next to Alice.
30+
Eric would lose 69 happiness units by sitting next to Bob.
31+
Eric would lose 33 happiness units by sitting next to Carol.
32+
Eric would lose 47 happiness units by sitting next to David.
33+
Eric would gain 75 happiness units by sitting next to Frank.
34+
Eric would gain 82 happiness units by sitting next to George.
35+
Eric would gain 13 happiness units by sitting next to Mallory.
36+
Frank would gain 77 happiness units by sitting next to Alice.
37+
Frank would gain 27 happiness units by sitting next to Bob.
38+
Frank would lose 87 happiness units by sitting next to Carol.
39+
Frank would gain 74 happiness units by sitting next to David.
40+
Frank would lose 41 happiness units by sitting next to Eric.
41+
Frank would lose 99 happiness units by sitting next to George.
42+
Frank would gain 26 happiness units by sitting next to Mallory.
43+
George would lose 63 happiness units by sitting next to Alice.
44+
George would lose 51 happiness units by sitting next to Bob.
45+
George would lose 60 happiness units by sitting next to Carol.
46+
George would gain 30 happiness units by sitting next to David.
47+
George would lose 100 happiness units by sitting next to Eric.
48+
George would lose 63 happiness units by sitting next to Frank.
49+
George would gain 57 happiness units by sitting next to Mallory.
50+
Mallory would lose 71 happiness units by sitting next to Alice.
51+
Mallory would lose 28 happiness units by sitting next to Bob.
52+
Mallory would lose 10 happiness units by sitting next to Carol.
53+
Mallory would gain 44 happiness units by sitting next to David.
54+
Mallory would gain 22 happiness units by sitting next to Eric.
55+
Mallory would gain 79 happiness units by sitting next to Frank.
56+
Mallory would lose 16 happiness units by sitting next to George.

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ pub mod year2015 {
172172
pub mod day10;
173173
pub mod day11;
174174
pub mod day12;
175+
pub mod day13;
175176
}
176177

177178
/// # Rescue Santa from deep space with a solar system adventure.

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ fn all_solutions() -> Vec<Solution> {
8888
solution!(year2015, day10),
8989
solution!(year2015, day11),
9090
solution!(year2015, day12),
91+
solution!(year2015, day13),
9192
// 2019
9293
solution!(year2019, day01),
9394
solution!(year2019, day02),

src/year2015/day13.rs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//! # Knights of the Dinner Table
2+
//!
3+
//! This problem is very similar to [`Day 9`] and we solve it in almost exactly the same way by
4+
//! computing an adjacency matrix of happiness then permuting the order of the diners.
5+
//!
6+
//! For part one we reduce the permutatations from 8! = 40,320 permutations to 7! = 5,040
7+
//! permutations by arbitrarily choosing one of the diners as the start.
8+
//!
9+
//! We solve part two at the same time by noticing that by inserting yourself between two diners
10+
//! you set the value of their mutual link to zero. Keeping tracking of the weakest link
11+
//! then subtracting that from the value for part one gives the result for part two at almost
12+
//! no additional cost.
13+
//!
14+
//! [`Day 9`]: crate::year2015::day09
15+
use crate::util::hash::*;
16+
use crate::util::parse::*;
17+
use crate::util::slice::*;
18+
19+
type Input = (i32, i32);
20+
21+
pub fn parse(input: &str) -> Input {
22+
// Assign each diner an index on a first come first served basis.
23+
let lines: Vec<Vec<_>> = input.lines().map(|line| line.split([' ', '.']).collect()).collect();
24+
let mut indices = FastMap::new();
25+
26+
for tokens in &lines {
27+
let size = indices.len();
28+
indices.entry(tokens[0]).or_insert(size);
29+
30+
let size = indices.len();
31+
indices.entry(tokens[10]).or_insert(size);
32+
}
33+
34+
// Calculate the happiness values. Note that the values are not reciprocal a => b != b => a.
35+
let stride = indices.len();
36+
let mut happiness = vec![0; stride * stride];
37+
38+
for tokens in &lines {
39+
let start = indices[tokens[0]];
40+
let end = indices[tokens[10]];
41+
let sign = if tokens[2] == "gain" { 1 } else { -1 };
42+
let value: i32 = tokens[3].signed();
43+
44+
// Add the values together to make the mutual link reciprocal
45+
happiness[stride * start + end] += sign * value;
46+
happiness[stride * end + start] += sign * value;
47+
}
48+
49+
// Solve both parts simultaneously.
50+
let mut part_one = 0;
51+
let mut part_two = 0;
52+
let mut indices: Vec<_> = (1..stride).collect();
53+
54+
indices.permutations(|slice| {
55+
let mut sum = 0;
56+
let mut weakest_link = i32::MAX;
57+
58+
let mut link = |from, to| {
59+
let value = happiness[stride * from + to];
60+
sum += value;
61+
weakest_link = weakest_link.min(value);
62+
};
63+
64+
link(0, slice[0]);
65+
link(0, slice[slice.len() - 1]);
66+
67+
for i in 1..slice.len() {
68+
link(slice[i], slice[i - 1]);
69+
}
70+
71+
part_one = part_one.max(sum);
72+
part_two = part_two.max(sum - weakest_link);
73+
});
74+
75+
(part_one, part_two)
76+
}
77+
78+
pub fn part1(input: &Input) -> i32 {
79+
input.0
80+
}
81+
82+
pub fn part2(input: &Input) -> i32 {
83+
input.1
84+
}

tests/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ mod year2015 {
4141
mod day10_test;
4242
mod day11_test;
4343
mod day12_test;
44+
mod day13_test;
4445
}
4546

4647
mod year2019 {

tests/year2015/day13_test.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use aoc::year2015::day13::*;
2+
3+
const EXAMPLE: &str = "\
4+
Alice would gain 54 happiness units by sitting next to Bob.
5+
Alice would lose 79 happiness units by sitting next to Carol.
6+
Alice would lose 2 happiness units by sitting next to David.
7+
Bob would gain 83 happiness units by sitting next to Alice.
8+
Bob would lose 7 happiness units by sitting next to Carol.
9+
Bob would lose 63 happiness units by sitting next to David.
10+
Carol would lose 62 happiness units by sitting next to Alice.
11+
Carol would gain 60 happiness units by sitting next to Bob.
12+
Carol would gain 55 happiness units by sitting next to David.
13+
David would gain 46 happiness units by sitting next to Alice.
14+
David would lose 7 happiness units by sitting next to Bob.
15+
David would gain 41 happiness units by sitting next to Carol.";
16+
17+
#[test]
18+
fn part1_test() {
19+
let input = parse(EXAMPLE);
20+
assert_eq!(part1(&input), 330);
21+
}
22+
23+
#[test]
24+
fn part2_test() {
25+
let input = parse(EXAMPLE);
26+
assert_eq!(part2(&input), 286);
27+
}

0 commit comments

Comments
 (0)