-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
98 lines (90 loc) · 2.91 KB
/
main.rs
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
#[macro_use]
extern crate lazy_static;
extern crate itertools;
extern crate regex;
use itertools::Itertools;
use regex::Regex;
use std::collections::{HashMap, HashSet};
lazy_static! {
static ref LINE_RE: Regex =
Regex::new(r"^(.+?) would (gain|lose) (\d+) happiness units by sitting next to (.+?)\.$")
.unwrap();
}
fn parse_line(s: &str) -> (String, String, isize) {
let m = LINE_RE.captures(s).unwrap();
return (
String::from(&m[1]),
String::from(&m[4]),
&m[3].parse::<isize>().unwrap() * if &m[2] == "gain" { 1 } else { -1 },
);
}
fn calc_max_happiness_gains(
names: HashSet<String>,
map: HashMap<(String, String), isize>,
) -> isize {
let name_count = &names.len();
names
.into_iter()
.permutations(*name_count)
.map(|arrangement| {
let happiness_gains: isize = arrangement
.windows(2)
.map(|pair| {
map.get(&(pair[0].clone(), pair[1].clone())).unwrap()
+ map.get(&(pair[1].clone(), pair[0].clone())).unwrap()
})
.sum();
let first = arrangement.get(0).unwrap();
let last = arrangement.get(arrangement.len() - 1).unwrap();
let edge_happiness = map.get(&(first.clone(), last.clone())).unwrap()
+ map.get(&(last.clone(), first.clone())).unwrap();
happiness_gains + edge_happiness
})
.max()
.unwrap()
}
fn part1(input: &str) -> isize {
let mut map = HashMap::new();
let mut names = HashSet::new();
for (a, b, gain) in input.split('\n').map(parse_line) {
map.insert((a.clone(), b.clone()), gain);
names.insert(a);
names.insert(b);
}
calc_max_happiness_gains(names, map)
}
fn part2(input: &str) -> isize {
let me = String::from("Me");
let mut map = HashMap::new();
let mut names = HashSet::new();
names.insert(me.clone());
for (a, b, gain) in input.split('\n').map(parse_line) {
map.insert((a.clone(), b.clone()), gain);
names.insert(a.clone());
names.insert(b.clone());
map.insert((a.clone(), me.clone()), 0);
map.insert((me.clone(), a.clone()), 0);
map.insert((b.clone(), me.clone()), 0);
map.insert((me.clone(), b.clone()), 0);
}
calc_max_happiness_gains(names, map)
}
fn main() {
println!("Part 1: {}", part1(include_str!("in.txt")));
println!("Part 2: {}", part2(include_str!("in.txt")));
}
#[test]
fn test_parse_line() {
assert_eq!(
parse_line("Alice would gain 54 happiness units by sitting next to Bob."),
("Alice".to_string(), "Bob".to_string(), 54)
);
assert_eq!(
parse_line("Alice would lose 79 happiness units by sitting next to Carol."),
("Alice".to_string(), "Carol".to_string(), -79)
);
}
#[test]
fn test_part1() {
assert_eq!(part1(include_str!("test.txt")), 330);
}