Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b935ac5

Browse filesBrowse files
2015, day 13
1 parent 3dd50c3 commit b935ac5
Copy full SHA for b935ac5

File tree

Expand file treeCollapse file tree

3 files changed

+93
-0
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+93
-0
lines changed

‎src/main.rs

Copy file name to clipboardExpand all lines: src/main.rs
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ fn main() {
4343
(2015, 10) => year_2015::day10::run("3113322113"),
4444
(2015, 11) => year_2015::day11::run("vzbxkghb"),
4545
(2015, 12) => year_2015::day12::run(&inputs::read_first_line(year, day)),
46+
(2015, 13) => year_2015::day13::run(inputs::read(year, day)),
4647
(2015, _) => println!("work in progress..."),
4748

4849
// 2016

‎src/year_2015/day13.rs

Copy file name to clipboard
+91Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
use regex::Regex;
2+
use std::collections::HashMap;
3+
use std::collections::HashSet;
4+
5+
pub fn run(input: Vec<String>) {
6+
let part1 = solve(&input, false);
7+
println!("part 1: {}", part1);
8+
assert_eq!(part1, 664);
9+
10+
let part2 = solve(&input, true);
11+
println!("part 2: {}", part2);
12+
assert_eq!(part2, 640);
13+
}
14+
15+
fn solve(input: &Vec<String>, extra: bool) -> i64 {
16+
// parse the input
17+
let mut happiness: HashMap<(&str, &str), i64> = HashMap::new();
18+
let mut people: HashSet<&str> = HashSet::new();
19+
20+
let re =
21+
Regex::new(r"^(.+?) would (gain|lose) (\d+?) happiness units by sitting next to (.+?)\.$")
22+
.unwrap();
23+
for line in input.iter() {
24+
match re.captures(line) {
25+
Some(cap) => {
26+
let p1 = cap.get(1).unwrap().as_str();
27+
let p2 = cap.get(4).unwrap().as_str();
28+
people.insert(p1);
29+
people.insert(p2);
30+
let sign = if cap.get(2).unwrap().as_str() == "gain" {
31+
1
32+
} else {
33+
-1
34+
};
35+
let units: i64 = cap.get(3).unwrap().as_str().parse().unwrap();
36+
happiness.insert((p1, p2), units * sign);
37+
}
38+
None => {
39+
panic!("invalid input: {}", line);
40+
}
41+
}
42+
}
43+
44+
// handle part2
45+
if extra {
46+
let you = "you";
47+
for p in people.iter() {
48+
happiness.insert((p, you), 0);
49+
happiness.insert((you, p), 0);
50+
}
51+
people.insert(you);
52+
}
53+
54+
// we "anchor" one person, because the table is round.
55+
let p = people.iter().next().unwrap().clone();
56+
let mut seen = HashSet::new();
57+
seen.insert(p);
58+
59+
// recursively explore the search space.
60+
let max = find_max(&happiness, p, p, &people, &mut seen);
61+
return max;
62+
}
63+
64+
fn find_max<'a>(happiness: &HashMap<(&'a str, &'a str), i64>,
65+
first: &'a str,
66+
prev: &'a str,
67+
people: &HashSet<&'a str>,
68+
seen: &mut HashSet<&'a str>)
69+
-> i64 {
70+
if seen.len() == people.len() {
71+
// we are done. We have to adjust for first and last people sitting next to each other
72+
return happiness.get(&(first, prev)).unwrap() + happiness.get(&(prev, first)).unwrap();
73+
}
74+
let mut max = None;
75+
for p in people.iter() {
76+
if seen.contains(p) {
77+
continue;
78+
}
79+
let p = p.clone();
80+
seen.insert(p);
81+
let mut v = find_max(happiness, first, p, people, seen);
82+
seen.remove(p);
83+
v += *happiness.get(&(prev, p)).unwrap();
84+
v += *happiness.get(&(p, prev)).unwrap();
85+
max = match max {
86+
None => Some(v),
87+
Some(max) => if v > max { Some(v) } else { Some(max) },
88+
}
89+
}
90+
return max.unwrap();
91+
}

‎src/year_2015/mod.rs

Copy file name to clipboardExpand all lines: src/year_2015/mod.rs
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ pub mod day09;
1010
pub mod day10;
1111
pub mod day11;
1212
pub mod day12;
13+
pub mod day13;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.