generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
25.rs
46 lines (38 loc) · 1.29 KB
/
25.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
use std::collections::{HashMap, HashSet};
advent_of_code::solution!(25);
pub fn part_one(input: &str) -> Option<usize> {
let mut G: HashMap<&str, HashSet<&str>> = HashMap::new();
input.lines().for_each(|line| {
let mut parts = line.split(": ");
let a = parts.next().unwrap();
let b = parts.next().unwrap().split(" ");
for c in b {
G.entry(a).or_insert(HashSet::new()).insert(c);
G.entry(c).or_insert(HashSet::new()).insert(a);
}
});
let mut S: HashSet<&str> = G.keys().cloned().collect();
let G_keys: HashSet<&str> = G.keys().cloned().collect();
while S.iter().map(|&v| G[v].difference(&S).count()).sum::<usize>() != 3 {
let &max_v = S.iter().max_by_key(|&&v| G[v].difference(&S).count()).unwrap();
S.remove(max_v);
}
Some(S.len() * (G_keys.difference(&S).count()))
}
pub fn part_two(input: &str) -> Option<u32> {
None
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(54));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, None);
}
}