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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
use advent_lib::prelude::*;
use std::collections::BTreeMap;
use std::collections::BTreeSet;
#[derive(Clone,Debug)]
struct Food {
ingredients: Vec<String>,
allergens: Vec<String>,
}
fn main() -> Result<()> {
let mut args = std::env::args();
if args.len() != 2 {
eprintln!("Usage: advent input");
}
let _ = args.next();
let filename = args.next().unwrap();
let input = advent_lib::read_lines_file(&filename)?;
let mut foods: Vec<Food> = Vec::new();
let mut ingredients: BTreeSet<String> = BTreeSet::new();
let mut allergens: BTreeSet<String> = BTreeSet::new();
let mut solved_ingredients: BTreeSet<String> = BTreeSet::new();
let mut solved_allergens: BTreeSet<String> = BTreeSet::new();
for line in &input {
let paren_point = line.find('(').unwrap();
let (ingredients_input, _) = line.split_at(paren_point - 1);
let (_, rest) = line.split_at(paren_point);
let mut food_ingredients = Vec::new();
for word in ingredients_input.split(' ') {
food_ingredients.push(word.to_string());
ingredients.insert(word.to_string());
}
let mut food_allergens = Vec::new();
let mut allergens_iterator = rest.split(' ');
let _ = allergens_iterator.next();
for word in allergens_iterator {
let (stripped_word, _) = word.split_at(word.len() - 1);
food_allergens.push(stripped_word.to_string());
allergens.insert(stripped_word.to_string());
}
foods.push(Food {
ingredients: food_ingredients,
allergens: food_allergens,
});
}
let mut ingredients_could_have_allergens: BTreeMap<String,BTreeSet<String>> =
BTreeMap::new();
for food in &foods {
for ingredient in &food.ingredients {
let mut allergen_set = BTreeSet::new();
for allergen in &food.allergens {
allergen_set.insert(allergen.clone());
}
match ingredients_could_have_allergens.get(ingredient) {
Some(previous_set) => {
for allergen in previous_set {
allergen_set.insert(allergen.to_string());
}
},
None => { }
}
ingredients_could_have_allergens.insert(ingredient.to_string(), allergen_set);
}
}
let mut allergens_could_be_ingredients: BTreeMap<String,BTreeSet<String>> =
BTreeMap::new();
for food in &foods {
for allergen in &food.allergens {
let mut ingredient_set = BTreeSet::new();
for ingredient in &food.ingredients {
ingredient_set.insert(ingredient.clone());
}
match allergens_could_be_ingredients.get(allergen) {
Some(previous_set) => {
let mut intersected_set = BTreeSet::new();
for ingredient in ingredient_set.intersection(previous_set) {
intersected_set.insert(ingredient.to_string());
}
ingredient_set = intersected_set;
},
None => { }
}
allergens_could_be_ingredients.insert(allergen.to_string(), ingredient_set);
}
}
loop {
let mut removed_any = false;
loop {
let mut mapping_to_remove: Option<(String,String)> = None;
for (allergen, possible_ingredients) in &allergens_could_be_ingredients {
if solved_allergens.contains(allergen) {
continue;
}
if possible_ingredients.len() == 1 {
let allergen = allergen.to_string();
let ingredient = possible_ingredients.iter().next().unwrap().to_string();
solved_ingredients.insert(ingredient.clone());
solved_allergens.insert(allergen.clone());
mapping_to_remove = Some((allergen, ingredient));
removed_any = true;
break;
}
}
match mapping_to_remove {
Some((allergen_to_remove, ingredient_to_remove)) => {
for allergen in &allergens {
if *allergen == allergen_to_remove {
continue;
}
let mut possible_ingredients =
allergens_could_be_ingredients.get(allergen).unwrap().clone();
possible_ingredients.remove(&ingredient_to_remove);
allergens_could_be_ingredients.insert(
allergen.to_string(), possible_ingredients);
}
for ingredient in &ingredients {
if *ingredient == ingredient_to_remove {
continue;
}
let mut possible_allergens =
ingredients_could_have_allergens.get(ingredient).unwrap().clone();
possible_allergens.remove(&allergen_to_remove);
ingredients_could_have_allergens.insert(
ingredient.to_string(), possible_allergens);
}
},
None => {
break;
},
}
}
loop {
let mut mapping_to_remove: Option<(String,String)> = None;
for (ingredient, possible_allergens) in &ingredients_could_have_allergens {
if solved_ingredients.contains(ingredient) {
continue;
}
if possible_allergens.len() == 1 {
let ingredient = ingredient.to_string();
let allergen = possible_allergens.iter().next().unwrap().to_string();
solved_ingredients.insert(ingredient.clone());
solved_allergens.insert(allergen.clone());
mapping_to_remove = Some((ingredient, allergen));
removed_any = true;
break;
}
}
match mapping_to_remove {
Some((ingredient_to_remove, allergen_to_remove)) => {
for allergen in &allergens {
if *allergen == allergen_to_remove {
continue;
}
let mut possible_ingredients =
allergens_could_be_ingredients.get(allergen).unwrap().clone();
possible_ingredients.remove(&ingredient_to_remove);
allergens_could_be_ingredients.insert(
allergen.to_string(), possible_ingredients);
}
for ingredient in &ingredients {
if *ingredient == ingredient_to_remove {
continue;
}
let mut possible_allergens =
ingredients_could_have_allergens.get(ingredient).unwrap().clone();
possible_allergens.remove(&allergen_to_remove);
ingredients_could_have_allergens.insert(
ingredient.to_string(), possible_allergens);
}
},
None => {
break;
},
}
}
if !removed_any {
break;
}
}
let mut ingredients_without_allergens = BTreeSet::new();
for ingredient in &ingredients {
let possible_allergens = ingredients_could_have_allergens.get(ingredient).unwrap();
if possible_allergens.len() == 0 {
ingredients_without_allergens.insert(ingredient.to_string());
}
}
let mut appearances = 0;
for food in &foods {
for ingredient in &food.ingredients {
if ingredients_without_allergens.contains(ingredient) {
appearances += 1;
}
}
}
println!("{}", appearances);
let mut dangerous_ingredients: Vec<String> = Vec::new();
let mut sorted_allergens = Vec::new();
for allergen in &allergens {
sorted_allergens.push(allergen);
}
sorted_allergens.sort();
for allergen in &sorted_allergens {
let ingredients = allergens_could_be_ingredients.get(&allergen.to_string()).unwrap();
if ingredients.len() == 1 {
dangerous_ingredients.push(ingredients.iter().next().unwrap().to_string());
}
}
let list = dangerous_ingredients.join(",");
println!("{}", list);
Ok(())
}
|