-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
48 lines (43 loc) · 1.32 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
use std::collections::HashMap;
fn parse_map(s: &str, sep: &'static str) -> HashMap<String, usize> {
let mut map: HashMap<String, usize> = HashMap::new();
for part in s.split(sep) {
let mut kv = part.trim().split(": ");
let key = String::from(kv.next().unwrap());
let value = kv.next().unwrap().parse::<usize>().unwrap();
map.insert(key, value);
}
map
}
fn parse_aunt(s: &str) -> (String, HashMap<String, usize>) {
let first_sep = s.find(':').unwrap();
(
s[0..first_sep].to_string(),
parse_map(&s[(first_sep + 1)..], ", "),
)
}
fn main() {
let compounds = parse_map(include_str!("compounds.txt"), "\n");
let aunts = include_str!("in.txt")
.lines()
.map(parse_aunt)
.collect::<Vec<_>>();
for (aunt, props) in aunts.clone() {
if props.iter().all(|(k, v)| compounds.get(k) == Some(v)) {
println!("Part 1: {}", aunt);
break;
}
}
for (aunt, props) in aunts {
if props.iter().all(|(k, v)| {
let val = compounds.get(k).unwrap();
match k.as_str() {
"cats" | "trees" => v > val,
"pomeranians" | "goldfish" => v < val,
_ => v == val,
}
}) {
println!("Part 2: {}", aunt);
}
}
}