-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.rs
36 lines (32 loc) · 1.04 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
use regex::Regex;
use std::collections::HashMap;
lazy_static::lazy_static! {
static ref RE_RULE: Regex = Regex::new(r#"^([a-z ]+) bags contain (.*)$"#).unwrap();
static ref RE_CONT: Regex = Regex::new(r#"(\d) ([a-z ]+) b"#).unwrap();
}
pub fn main() {
let rules: HashMap<_, _> = include_str!("../input.txt")
.lines()
.map(parse_bag)
.collect();
println!("{}", bags("shiny gold", &rules) - 1);
}
/// Parse bag ruleset.
#[inline(always)]
fn parse_bag<'a>(rule: &'a str) -> (&'a str, Vec<(&str, usize)>) {
let captures = RE_RULE.captures(rule).unwrap();
(
captures.get(1).unwrap().as_str(),
RE_CONT
.captures_iter(captures.get(2).unwrap().as_str())
.map(|cond| (cond.get(2).unwrap().as_str(), cond[1].parse().unwrap()))
.collect(),
)
}
/// Count bags in bags.
fn bags(color: &str, rules: &HashMap<&str, Vec<(&str, usize)>>) -> usize {
1 + rules[color]
.iter()
.map(|(color, count)| bags(color, rules) * count)
.sum::<usize>()
}