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
use crate::input::Input;
use std::collections::{HashMap, HashSet};
pub fn solve(input: &mut Input) -> Result<String, String> {
let on_error = || "Invalid input";
let mut allergen_to_idx = HashMap::with_capacity(8);
let mut allergen_names = Vec::with_capacity(8);
let mut ingredient_to_idx = HashMap::with_capacity(200);
let mut ingredient_names = Vec::with_capacity(200);
let mut ingredient_occurences: Vec<u16> = Vec::with_capacity(200);
let mut allergen_to_possible_ingredients = Vec::with_capacity(8);
for line in input.text.lines() {
let mut line_parts = line.splitn(2, " (contains ");
let ingredients = line_parts.next().ok_or_else(on_error)?;
let mut current_ingredients = HashSet::new();
for ingredient_name in ingredients.split(' ') {
let num_ingredients = ingredient_to_idx.len();
let ingredient_id = *ingredient_to_idx
.entry(ingredient_name)
.or_insert(num_ingredients);
if ingredient_id == ingredient_occurences.len() {
ingredient_occurences.push(1);
ingredient_names.push(ingredient_name);
} else {
ingredient_occurences[ingredient_id] += 1;
}
current_ingredients.insert(ingredient_id);
}
if let Some(allergens) = line_parts.next().ok_or_else(on_error)?.strip_suffix(')') {
for allergen_name in allergens.split(", ") {
let num_allergens = allergen_to_idx.len();
let allergen_id = *allergen_to_idx
.entry(allergen_name)
.or_insert(num_allergens);
if allergen_id == allergen_names.len() {
allergen_names.push(allergen_name);
allergen_to_possible_ingredients.push(current_ingredients.clone());
} else {
let existing = &allergen_to_possible_ingredients[allergen_id];
allergen_to_possible_ingredients[allergen_id] = current_ingredients
.intersection(existing)
.copied()
.collect();
}
}
} else {
return Err(on_error().to_string());
}
}
if input.is_part_one() {
return Ok((0..ingredient_to_idx.len())
.filter_map(|ingredient_id| {
if allergen_to_possible_ingredients
.iter()
.any(|possible| possible.contains(&ingredient_id))
{
None
} else {
Some(u64::from(ingredient_occurences[ingredient_id]))
}
})
.sum::<u64>()
.to_string());
}
let mut identified_products = allergen_to_possible_ingredients
.iter()
.filter_map(|possibilities| {
if possibilities.len() == 1 {
possibilities.iter().next().copied()
} else {
None
}
})
.collect::<Vec<usize>>();
while let Some(product_id) = identified_products.pop() {
for possibilities in allergen_to_possible_ingredients.iter_mut() {
if possibilities.len() > 1
&& possibilities.remove(&product_id)
&& possibilities.len() == 1
{
let p = possibilities
.iter()
.next()
.ok_or_else(|| "Internal error".to_string())?;
identified_products.push(*p);
}
}
}
let mut ingredient_and_allergents = allergen_to_possible_ingredients
.iter()
.enumerate()
.map(
|(idx, possible_ingredients)| -> Result<(usize, usize), String> {
Ok((
*possible_ingredients
.iter()
.next()
.ok_or_else(|| "Internal error".to_string())?,
idx,
))
},
)
.collect::<Result<Vec<_>, _>>()?;
ingredient_and_allergents.sort_unstable_by(|a, b| {
let a_allergen_name = allergen_names[a.1];
let b_allergen_name = allergen_names[b.1];
a_allergen_name.cmp(b_allergen_name)
});
Ok(ingredient_and_allergents
.iter()
.map(|&(ingredient_id, _)| ingredient_names[ingredient_id])
.collect::<Vec<_>>()
.join(","))
}
#[test]
pub fn tests() {
use crate::input::{test_part_one, test_part_two};
let example = "mxmxvkd kfcds sqjhc nhms (contains dairy, fish)
trh fvjkl sbzzf mxmxvkd (contains dairy)
sqjhc fvjkl (contains soy)
sqjhc mxmxvkd sbzzf (contains fish)";
test_part_one!(example => "5".to_string());
test_part_two!(example => "mxmxvkd,sqjhc,fvjkl".to_string());
let other_input = include_str!("day21_input_other.txt");
test_part_one!(other_input => "2724".to_string());
test_part_two!(other_input => "xlxknk,cskbmx,cjdmk,bmhn,jrmr,tzxcmr,fmgxh,fxzh".to_string());
let real_input = include_str!("day21_input.txt");
test_part_one!(real_input => "2317".to_string());
test_part_two!(real_input => "kbdgs,sqvv,slkfgq,vgnj,brdd,tpd,csfmb,lrnz".to_string());
}