-
Notifications
You must be signed in to change notification settings - Fork 0
/
2020_21.cs
268 lines (220 loc) · 7.58 KB
/
2020_21.cs
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
namespace AOC.CSharp;
public static class AOC2020_21
{
public static Tuple<long, string> Solve(string[] rawLines, bool part1Only)
{
long ans1 = 0;
Counter counter = new();
IEnumerable<Line> lines = rawLines.Select(Line.Parse);
HashSet<string> ingredients = lines.SelectMany(line => line.Ingredients).ToHashSet();
HashSet<string> allergens = lines.SelectMany(line => line.Allergens).ToHashSet();
foreach (var line in lines)
{
foreach (var allergen in line.Allergens)
{
counter.Increment(allergen);
foreach (var ingredient in line.Ingredients)
{
string key = allergen + "_" + ingredient;
counter.Increment(key);
}
}
foreach (var ingredient in line.Ingredients)
{
counter.Increment(ingredient);
}
}
foreach (string ingredient in ingredients.ToArray())
{
bool valid = false;
foreach (string allergen in allergens)
{
string key = allergen + "_" + ingredient;
int allergenIngredientCount = counter.GetCount(key);
int allergenCount = counter.GetCount(allergen);
bool match = allergenIngredientCount == allergenCount;
valid |= match;
}
if (!valid)
{
int ingredientCount = counter.GetCount(ingredient);
ans1 += ingredientCount;
ingredients.Remove(ingredient);
}
}
if (part1Only)
{
return Tuple.Create<long, string>(ans1, null);
}
// Build part 2 matrix with only the valid ingredients
ResolutionMatrix matrix = new(ingredients.ToArray(), allergens.ToArray());
foreach (string ingredient in ingredients)
{
foreach (string allergen in allergens)
{
string key = allergen + "_" + ingredient;
int allergenIngredientCount = counter.GetCount(key);
int allergenCount = counter.GetCount(allergen);
bool match = allergenIngredientCount == allergenCount;
if (match)
{
matrix.Set(ingredient, allergen);
}
else
{
matrix.Clear(ingredient, allergen);
}
}
}
matrix.Resolve();
string ans2 = string.Join(
",",
matrix.GetFinalPairs().OrderBy(p => p.Item2).Select(p => p.Item1)
);
return Tuple.Create<long, string>(0L, ans2);
}
public static long Solve1(string[] lines)
{
return Solve(lines, true).Item1;
}
public static string Solve2(string[] lines)
{
return Solve(lines, false).Item2;
}
private class ResolutionMatrix
{
private bool[,] _matrix;
private Dictionary<string, int> _ingredientIndices = new();
private Dictionary<string, int> _allergenIndices = new();
private Dictionary<int, string> _ingredientNames = new();
private Dictionary<int, string> _allergenNames = new();
public ResolutionMatrix(string[] ingredients, string[] allergens)
{
for (int i = 0; i < ingredients.Length; i++)
{
_ingredientIndices[ingredients[i]] = i;
_ingredientNames[i] = ingredients[i];
}
for (int i = 0; i < allergens.Length; i++)
{
_allergenIndices[allergens[i]] = i;
_allergenNames[i] = allergens[i];
}
_matrix = new bool[ingredients.Length, allergens.Length];
}
public void Clear(string ingredient, string allergen)
{
int ingredientIdx = _ingredientIndices[ingredient];
int allergenIdx = _allergenIndices[allergen];
_matrix[ingredientIdx, allergenIdx] = false;
}
public void Set(string ingredient, string allergen)
{
int ingredientIdx = _ingredientIndices[ingredient];
int allergenIdx = _allergenIndices[allergen];
_matrix[ingredientIdx, allergenIdx] = true;
}
public void Resolve()
{
bool changed = true;
while (changed)
{
changed = false;
for (int i = 0; i < _matrix.GetLength(0); i++)
{
int? onlyAllergen = FindOnlyAllergen(i);
if (onlyAllergen != null)
{
changed |= LockInAllergen(i, onlyAllergen.Value);
}
}
}
}
public List<Tuple<string, string>> GetFinalPairs()
{
List<Tuple<string, string>> pairs = new();
for (int i = 0; i < _matrix.GetLength(0); i++)
{
for (int j = 0; j < _matrix.GetLength(1); j++)
{
if (_matrix[i, j])
{
string ingredient = _ingredientNames[i];
string allergen = _allergenNames[j];
pairs.Add(Tuple.Create(ingredient, allergen));
}
}
}
return pairs;
}
private int? FindOnlyAllergen(int ingredientIdx)
{
int? foundIdx = null;
for (int i = 0; i < _matrix.GetLength(1); i++)
{
bool allergenValue = _matrix[ingredientIdx, i];
if (foundIdx == null && allergenValue)
{
foundIdx = i;
}
else if (allergenValue)
{
return null;
}
}
return foundIdx;
}
private bool LockInAllergen(int ingredientIdx, int allergenIdx)
{
bool changed = false;
for (int i = 0; i < _matrix.GetLength(0); i++)
{
if (i != ingredientIdx)
{
if (_matrix[i, allergenIdx] == true)
{
_matrix[i, allergenIdx] = false;
changed = true;
}
}
}
return changed;
}
}
private class Counter
{
Dictionary<string, int> _counts = new Dictionary<string, int>();
public void Increment(string key)
{
if (!_counts.TryGetValue(key, out _))
{
_counts.Add(key, 1);
}
else
{
_counts[key] = _counts[key] + 1;
}
}
public int GetCount(string key)
{
return _counts.TryGetValue(key, out int value) ? value : 0;
}
}
private class Line
{
public static Line Parse(string line)
{
var splits = line.Replace(")", "")
.Replace("(contains ", "|")
.Replace(",", "")
.Split('|', StringSplitOptions.RemoveEmptyEntries);
return new Line
{
Ingredients = splits[0].Split(' ', StringSplitOptions.RemoveEmptyEntries).ToList(),
Allergens = splits[1].Split(' ', StringSplitOptions.RemoveEmptyEntries).ToList(),
};
}
public List<string> Ingredients { get; init; }
public List<string> Allergens { get; init; }
}
}