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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
use std::collections::HashMap;
use regex::Regex;
fn main() {
let input = advent::read_lines(16);
let search = HashMap::from([
("children", 3),
("cats", 7),
("samoyeds", 2),
("pomeranians", 3),
("akitas", 0),
("vizslas", 0),
("goldfish", 5),
("trees", 3),
("cars", 2),
("perfumes", 1),
]);
println!("16a: {}", find_aunt(&input, &search, false));
println!("16b: {}", find_aunt(&input, &search, true));
}
struct Aunt {
name: u16,
attributes: HashMap<String, u8>,
}
impl Aunt {
fn new(input: &str) -> Aunt {
let re = Regex::new(r"([a-z]+): ([0-9]+)").unwrap();
let re_aunt = Regex::new(r"^Sue ([0-9]+):").unwrap();
let name = re_aunt.captures(input).unwrap()[1].parse::<u16>().unwrap();
let mut aunt = Aunt {
name,
attributes: HashMap::new(),
};
for cap in re.captures_iter(input) {
let attribute = cap[1].to_string();
let amount = cap[2].parse::<u8>().unwrap();
aunt.attributes.insert(attribute, amount);
}
aunt
}
fn has_attributes(&self, attributes: &HashMap<&str, u8>, outdated: bool) -> bool {
if outdated {
self.has_attributes_v2(attributes)
} else {
self.has_attributes_v1(attributes)
}
}
fn has_attributes_v1(&self, attributes: &HashMap<&str, u8>) -> bool {
for (attr, val) in attributes {
if let Some(myval) = self.attributes.get(*attr) {
if myval != val {
/* found non-matching attribute */
return false;
}
}
}
true
}
fn has_attributes_v2(&self, attributes: &HashMap<&str, u8>) -> bool {
for (attr, val) in attributes {
if let Some(myval) = self.attributes.get(*attr) {
match attr {
x if ["cats", "trees"].contains(x) => if myval <= val { return false; },
x if ["pomeranians", "goldfish"].contains(x) => if myval >= val { return false; },
_ => if myval != val { return false; }
}
}
}
true
}
}
fn find_aunt(input: &[String], search: &HashMap<&str, u8>, outdated: bool) -> u16 {
input.iter()
.map(|x| Aunt::new(x))
.find(|x| x.has_attributes(search, outdated))
.unwrap()
.name
}
|