-
Notifications
You must be signed in to change notification settings - Fork 0
/
quest06.rs
49 lines (39 loc) · 1.24 KB
/
quest06.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
use std::collections::HashMap;
pub fn part1(notes: &str) -> String {
solve(notes, false)
}
pub fn part2(notes: &str) -> String {
solve(notes, true)
}
pub fn part3(notes: &str) -> String {
solve(notes, true)
}
fn solve(notes: &str, first_letter_only: bool) -> String {
let mut apples = Vec::new();
let mut parents = HashMap::new();
let mut branches = HashMap::new();
for line in notes.lines() {
let (parent, children) = line.split_once(':').unwrap();
for child in children.split(',') {
if child == "@" {
apples.push(parent);
} else {
parents.insert(child, parent);
}
}
}
apples.iter().for_each(|apple| {
let mut current = apple;
let mut path = vec!["@", apple];
while let Some(next) = parents.get(current) {
if path.contains(next) {
return;
}
current = next;
path.push(next);
}
branches.entry(path.len()).or_insert_with(Vec::new).push(path);
});
let powerful = branches.values().filter(|p| p.len() == 1).flatten().next().unwrap();
powerful.iter().rev().map(|p| if first_letter_only { &p[..1] } else { p }).collect()
}