-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2020_day7.rs
64 lines (50 loc) · 1.72 KB
/
2020_day7.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
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::collections::HashMap;
#[derive(Debug)]
struct BagRule {
quantity: u32,
color: String,
}
fn read_entries(line: &str) -> (String, Vec<BagRule>) {
let parts: Vec<&str> = line.split(" bags contain ").collect();
let container = parts[0].to_string();
if parts[1].contains("no other bags") {
return (container, Vec::new());
}
let contents: Vec<BagRule> = parts[1]
.split(", ")
.map(|rule| {
let words: Vec<&str> = rule.split_whitespace().collect();
let quantity = words[0].parse().unwrap();
let color = format!("{} {}", words[1], words[2]);
BagRule { quantity, color }
})
.collect();
(container, contents)
}
fn count_bags_inside(color: &str, rules: &HashMap<String, Vec<BagRule>>) -> u32 {
let mut total = 0;
if let Some(contents) = rules.get(color) {
for bag in contents {
// Add the bags directly inside
total += bag.quantity;
// Add the bags inside each of those bags
total += bag.quantity * count_bags_inside(&bag.color, rules);
}
}
total
}
fn main() {
let file = File::open("input/day7.txt").unwrap();
let reader = BufReader::new(file);
let mut rules: HashMap<String, Vec<BagRule>> = HashMap::new();
// Parse all rules
for line in reader.lines() {
let line = line.unwrap();
let (container, contents) = read_entries(&line);
rules.insert(container, contents);
}
let total_bags = count_bags_inside("shiny gold", &rules);
println!("Number of bags required inside a single shiny gold bag: {}", total_bags);
}