-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.rs
67 lines (60 loc) · 2.01 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
use std::{collections::HashMap, fs};
fn get_input(fp: &str) -> Vec<Vec<String>> {
fs::read_to_string(fp)
.expect("Cannot read file")
.trim_end()
.split('\n')
.map(|x| x.split(')').map(String::from).collect())
.collect()
}
fn get_tree(input: Vec<Vec<String>>) -> (HashMap<String, Vec<String>>, HashMap<String, String>) {
let mut tree: HashMap<String, Vec<String>> = HashMap::new();
let mut parent: HashMap<String, String> = HashMap::new();
for x in input.into_iter() {
if tree.contains_key(&x[0]) {
tree.get_mut(&x[0]).unwrap().push(x[1].clone());
} else {
tree.insert(x[0].clone(), vec![x[1].clone()]);
}
tree.entry(x[1].clone()).or_insert_with(|| vec![]);
parent.insert(x[1].clone(), x[0].clone());
}
(tree, parent)
}
fn count(x: &str, current: usize, total: &mut usize, tree: &HashMap<String, Vec<String>>) -> usize {
*total += current;
for y in tree.get(x).unwrap().iter() {
count(y, current+1, total, tree);
}
*total
}
fn get_parents(x: &str, parent: &HashMap<String, String>) -> Vec<String> {
let mut parents = Vec::new();
let mut y = parent.get(x).unwrap();
while y != "COM" {
parents.push(y.to_string());
y = parent.get(y).unwrap();
}
parents
}
fn part_one(tree: &HashMap<String, Vec<String>>) -> usize{
count("COM", 0, &mut 0, tree)
}
fn part_two(parent: &HashMap<String, String>) -> usize {
let parents_YOU = get_parents("YOU", parent);
let parents_SAN = get_parents("SAN", parent);
let mut common = "";
for x in parents_YOU.iter() {
if parents_SAN.contains(x) {
common = x;
break;
}
}
parents_YOU.iter().position(|x| x==common).unwrap() + parents_SAN.iter().position(|x| x==common).unwrap()
}
fn main() {
let input = get_input("../input.txt");
let (tree, parent) = get_tree(input);
println!("Part one: {}", part_one(&tree));
println!("Part two: {}", part_two(&parent));
}