1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use itertools::Itertools;
use regex::Regex;
use std::collections::HashMap;
#[aoc_generator(day13)]
fn parse_input(input: &str) -> anyhow::Result<TableRuleset> {
let mut names: Vec<String> = Vec::new();
let mut rules: Rules = HashMap::new();
let re = Regex::new(
r"^(?P<left>\w+) would (?P<delta_type>gain|lose) (?P<delta>\d+) happiness units by sitting next to (?P<right>\w+).$",
)?;
for line in input.lines() {
if let Some(matches) = re.captures(line) {
let left = matches.name("left").unwrap().as_str().to_string();
let right = matches.name("right").unwrap().as_str().to_string();
let delta_type = matches.name("delta_type").unwrap().as_str();
let mut delta = matches.name("delta").unwrap().as_str().parse()?;
if delta_type == "lose" {
delta *= -1;
}
if !names.contains(&left) {
names.push(left.clone());
}
if !names.contains(&right) {
names.push(right.clone());
}
if !rules.contains_key(&left) {
rules.insert(left.clone(), HashMap::new());
}
rules.get_mut(&left).unwrap().insert(right.clone(), delta);
} else {
panic!("failed to parse: {}", line)
}
}
Ok(TableRuleset { names, rules })
}
#[aoc(day13, part1)]
fn part1(input: &TableRuleset) -> i64 {
input.best_happiness()
}
#[aoc(day13, part2)]
fn part2(input: &TableRuleset) -> i64 {
add_yourself(input).best_happiness()
}
type Rules = HashMap<String, HashMap<String, i64>>;
struct TableRuleset {
rules: Rules,
names: Vec<String>,
}
impl TableRuleset {
fn best_happiness(&self) -> i64 {
let mut best = i64::MIN;
for perm in self.names.iter().permutations(self.names.len()) {
let happiness = calc_happiness(&perm, &self.rules);
if happiness > best {
best = happiness;
}
}
best
}
}
fn add_yourself(rules: &TableRuleset) -> TableRuleset {
let mut names = rules.names.clone();
let mut rules = rules.rules.clone();
names.push("me".into());
rules.insert("me".into(), HashMap::new());
TableRuleset { names, rules }
}
fn get_delta(rules: &Rules, left: &str, right: &str) -> i64 {
*rules.get(left).unwrap().get(right).unwrap_or(&0)
}
fn calc_happiness(seating: &Vec<&String>, rules: &Rules) -> i64 {
let mut by_name: HashMap<String, i64> = HashMap::new();
for name in seating {
by_name.insert((*name).clone(), 0);
}
for pair in seating.windows(2) {
*by_name.get_mut(pair[0]).unwrap() += get_delta(rules, pair[0], pair[1]);
*by_name.get_mut(pair[1]).unwrap() += get_delta(rules, pair[1], pair[0]);
}
let last_idx = seating.len() - 1;
*by_name.get_mut(seating[0]).unwrap() += get_delta(rules, seating[0], seating[last_idx]);
*by_name.get_mut(seating[last_idx]).unwrap() += get_delta(rules, seating[last_idx], seating[0]);
by_name.values().sum()
}
#[cfg(test)]
mod tests {
use super::*;
const EXAMPLE: &str = "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.";
#[test]
fn part1_examples() {
let rules = parse_input(EXAMPLE).expect("failed to parse");
assert_eq!(rules.best_happiness(), 330);
}
}