-
Notifications
You must be signed in to change notification settings - Fork 2
/
21.c
297 lines (254 loc) · 6.9 KB
/
21.c
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include <assert.h>
#include <err.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "inputs/21.h"
// #include "inputs/21_test.h"
#define MAX_NAME_LENGTH 32
typedef struct {
char (*ingredients)[MAX_NAME_LENGTH];
int ningredients;
char (*allergens)[MAX_NAME_LENGTH];
int nallergens;
} food_t;
typedef struct {
char* name;
char** options;
int noptions;
} allergen_t;
static void* emalloc(int size) {
void* ptr = malloc(size);
if (!ptr) {
err(EXIT_FAILURE, "error allocating memory");
}
return ptr;
}
static void* erealloc(void* ptr, int size) {
ptr = realloc(ptr, size);
if (!ptr) {
err(EXIT_FAILURE, "error allocating memory");
}
return ptr;
}
static int
parse_input(food_t* restrict dest) {
const unsigned char *s = input;
int n = 0;
int i_cap = 16;
int a_cap = 8;
// mxmxvkd kfcds sqjhc nhms (contains dairy, fish)
while (*s != '\0') {
food_t* f = &dest[n++];
f->ningredients = 0;
f->nallergens = 0;
f->ingredients = emalloc(i_cap * MAX_NAME_LENGTH * sizeof(char));
f->allergens = emalloc(a_cap * MAX_NAME_LENGTH * sizeof(char));
while (*s != '(') {
if (f->ningredients == i_cap) {
i_cap *= 2;
f->ingredients =
erealloc(f->ingredients, i_cap * MAX_NAME_LENGTH * sizeof(char));
}
// parse ingredient name
char* i = f->ingredients[f->ningredients++];
while (*s != ' ') {
*i++ = *s++;
}
*i = '\0';
s++; // skip ' '
}
s += strlen("(contains "); // skip forward to first allergen
while (*s != ')') {
if (f->nallergens == a_cap) {
a_cap *= 2;
f->allergens =
erealloc(f->allergens, a_cap * MAX_NAME_LENGTH * sizeof(char));
}
// parse allergen name
char* a = f->allergens[f->nallergens++];
while (*s != ',' && *s != ')') {
*a++ = *s++;
}
*a = '\0';
// skip , and space
if (*s == ',' && *(s + 1) == ' ')
s += 2;
}
s++; // ')'
if (*s == '\n') s++;
}
return n;
}
__attribute((unused))
static void
print_food(const food_t* f) {
for (int j = 0; j < f->ningredients; j++) {
printf("%s ", f->ingredients[j]);
}
if (f->nallergens > 0) {
printf("(contains ");
for (int j = 0; j < f->nallergens; j++) {
if (j > 0) {
printf(", ");
}
printf("%s", f->allergens[j]);
}
printf(")");
}
printf("\n");
}
static bool
food_has_ingredient(const food_t* f, const char* ingredient) {
for (int i = 0; i < f->ningredients; i++) {
if (strcmp(f->ingredients[i], ingredient) == 0) {
return true;
}
}
return false;
}
static allergen_t*
get_allergen(allergen_t* list, int size, const char* name) {
for (int i = 0; i < size; i++) {
if (strcmp(list[i].name, name) == 0) {
return &list[i];
}
}
return NULL;
}
// returns true if this ingredient is a possible option for any allergen
static bool
ingredient_can_contain_allergen(const allergen_t * list,
int size,
const char* ingredient) {
for (int i = 0; i < size; i++) {
const allergen_t* a = &list[i];
for (int j = 0; j < a->noptions; j++) {
if (strcmp(a->options[j], ingredient) == 0) {
return true;
}
}
}
return false;
}
static int
cmp_allergen(void const* p1, void const* p2) {
const allergen_t* a = (allergen_t*)p1;
const allergen_t* b = (allergen_t*)p2;
return strcmp(a->name, b->name);
}
static void
remove_option_from_allergen(allergen_t* restrict a, int index) {
a->options[index] = a->options[a->noptions - 1];
a->noptions--;
}
int day21(void) {
food_t foods[32];
int nfoods = parse_input(foods);
// go over each allergen, finding intersection of ingredients as we go
int nallergens = 0;
allergen_t allergen_list[32];
for (int i = 0; i < nfoods; i++) {
food_t* f = &foods[i];
for (int j = 0; j < f->nallergens; j++) {
allergen_t* a = get_allergen(allergen_list, nallergens, f->allergens[j]);
if (a == NULL) {
a = &allergen_list[nallergens++];
a->name = f->allergens[j];
a->options = emalloc(f->ningredients * sizeof(char*));
a->noptions = f->ningredients;
for (int k = 0; k < f->ningredients; k++) {
a->options[k] = f->ingredients[k];
}
} else {
// we've seen allergen before,
// remove all options which are not an ingredient for current
// food
for (int32_t k = a->noptions - 1; k >= 0; k--) {
if (!food_has_ingredient(f, a->options[k])) {
remove_option_from_allergen(a, k);
}
}
}
}
}
// // print options
// for (int i=0; i < nallergens; i++) {
// printf("%s: ", allergen_list[i].name);
// for (int j=0; j < allergen_list[i].noptions; j++) {
// printf("%s, ", allergen_list[i].options[j]);
// }
// printf("\n");
// }
// then, find ingredients not in any of the allergen options
int64_t count = 0;
for (int i = 0; i < nfoods; i++) {
food_t* f = &foods[i];
for (int j = 0; j < f->ningredients; j++) {
const char* ingredient = f->ingredients[j];
if (ingredient_can_contain_allergen(allergen_list, nallergens,
ingredient)) {
continue;
}
count++;
}
}
printf("%ld\n", count);
assert(count == 1685);
// find each allergen with only 1 option
// remove this option from all other allergens, repeat until stable
for (int i = 0; i < nallergens; i++) {
allergen_t* a = &allergen_list[i];
if (a->noptions != 1) {
continue;
}
const char* ingredient = a->options[0];
bool stable = true;
for (int j = 0; j < nallergens; j++) {
// skip self
if (j == i)
continue;
allergen_t* a2 = &allergen_list[j];
for (int k = 0; k < a2->noptions; k++) {
if (strcmp(a2->options[k], ingredient) == 0) {
remove_option_from_allergen(a2, k);
stable = false;
}
}
}
// repeat until stable
if (stable == false) {
i = 0;
continue;
}
}
// sort alphabetically by allergen (so not ingredient name)
qsort(allergen_list, nallergens, sizeof(allergen_t), cmp_allergen);
// copy every option to answer string
char answer[BUFSIZ] = { '\0' };
char *s = answer;
for (int i = 0; i < nallergens; i++) {
if (i > 0) {
*s++ = ',';
}
char *s2 = allergen_list[i].options[0];
while (*s2 != '\0') {
*s++ = *s2++;
}
*s = '\0';
}
printf("%s\n", answer);
assert(strcmp(answer, "ntft,nhx,kfxr,xmhsbd,rrjb,xzhxj,chbtp,cqvc") == 0);
// free foods, ingredients and allergens
for (int i = 0; i < nallergens; i++) {
free(allergen_list[i].options);
}
for (int i = 0; i < nfoods; i++) {
free(foods[i].allergens);
free(foods[i].ingredients);
}
return 0;
}