-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
95 lines (84 loc) · 2.63 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#[macro_use]
extern crate lazy_static;
extern crate regex;
use regex::Regex;
use std::collections::{HashMap, HashSet};
fn parse_line(s: &str) -> (Vec<String>, Vec<String>) {
lazy_static! {
static ref RE: Regex = Regex::new(r"(.+) \(contains (.+)\)").unwrap();
}
let m = RE.captures(s).unwrap();
(
m[1].split(' ').map(String::from).collect::<Vec<String>>(),
m[2].split(", ").map(String::from).collect::<Vec<String>>(),
)
}
fn solve(s: &str) -> (usize, String) {
let mut map: HashMap<String, HashSet<String>> = HashMap::new();
let mut all_ingredients: Vec<String> = vec![];
let input = s.lines().map(parse_line);
for (ingredients, allergens) in input.clone() {
all_ingredients.extend_from_slice(&ingredients[..]);
let i = ingredients.iter().cloned().collect::<HashSet<String>>();
for a in allergens {
if map.contains_key(&a) {
let v = map.get_mut(&a).unwrap();
*v = v
.intersection(&i.clone())
.cloned()
.collect::<HashSet<String>>();
} else {
map.insert(a, i.clone());
}
}
}
let inv = map
.values()
.flat_map(|v| v.iter().map(|s| s.clone()))
.collect::<HashSet<String>>();
let part1 = all_ingredients
.iter()
.filter(|i| !(&inv).contains(i.clone()))
.count();
let mut known = HashMap::new();
loop {
let mut found = vec![];
for (allergen, possible_ingredients) in map.clone() {
if possible_ingredients.len() == 1 {
let ingredient = possible_ingredients.iter().next().unwrap();
found.push((allergen.clone(), ingredient.clone()));
known.insert(allergen.clone(), ingredient.clone());
}
}
if found.len() > 0 {
for (a, i) in found {
map.remove(&a);
for val in map.values_mut() {
val.remove(&i);
}
}
} else {
break;
}
}
let mut allergens = known.keys().cloned().collect::<Vec<String>>();
allergens.sort();
let part2 = allergens
.iter()
.map(|a| known.get(a).unwrap().clone())
.collect::<Vec<String>>()
.join(",");
(part1, part2)
}
fn main() {
let (part1, part2) = solve(include_str!("in.txt"));
println!("Part 1: {}", part1);
println!("Part 2: {}", part2);
}
#[test]
fn test_input() {
assert_eq!(
solve(include_str!("test.txt")),
(5, String::from("mxmxvkd,sqjhc,fvjkl"))
)
}