-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday21a.c
170 lines (139 loc) · 5.46 KB
/
day21a.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
#include "common.h"
#include "glossary.h"
#include "stringview.h"
#include "set.h"
#include "map.h"
struct Food {
Set_t ingredients;
Set_t allergens;
};
int main (int argc, char ** argv) {
Glossary_t glossary;
glossary_init(&glossary);
size_t food_count = 0;
struct Food food [64];
Set_t ingredients;
set_init(&ingredients, 0);
Set_t allergens;
set_init(&allergens, 0);
do {
char buf [BUFSIZ];
char * s = fgets(&(buf[0]), sizeof(buf), stdin);
if (!s) {
break;
}
StringView_t line = sv_view_c_string(&(buf[0]));
sv_eat_spaces(&line);
if (sv_is_empty(&line)) {
continue;
}
StringView_t ingredient_list = sv_eat_until(&line, '(');
ASSERT(line.start[0] == '(');
sv_eat_char(&line);
StringView_t allergen_list = sv_eat_until(&line, ')');
ASSERT(line.start[0] == ')');
StringView_t contains = sv_eat_until_space(&allergen_list);
ASSERT(sv_equals(&contains, "contains"));
sv_eat_spaces(&allergen_list);
Set_t this_ingredients;
set_init(&this_ingredients, 0);
Set_t this_allergens;
set_init(&this_allergens, 0);
while (!sv_is_empty(&ingredient_list)) {
StringView_t ingredient = sv_eat_until_space(&ingredient_list);
sv_eat_spaces(&ingredient_list);
intptr_t ingredient_id = glossary_add(&glossary, ingredient);
set_add(&ingredients, ingredient_id);
set_add(&this_ingredients, ingredient_id);
}
while (!sv_is_empty(&allergen_list)) {
StringView_t allergen = sv_eat_until_punctuation(&allergen_list);
if (allergen_list.start[0] == ',') {
sv_eat_char(&allergen_list);
}
sv_eat_spaces(&allergen_list);
intptr_t allergen_id = glossary_add(&glossary, allergen);
set_add(&allergens, allergen_id);
set_add(&this_allergens, allergen_id);
}
ASSERT(food_count < ARRAYCOUNT(food));
food[food_count].ingredients = this_ingredients;
food[food_count].allergens = this_allergens;
food_count += 1;
} while (1);
/*
Set_t allergen_free_ingredients;
set_init(&allergen_free_ingredients, 0);
for (intptr_t const * it = set_cbegin(&ingredients); it != set_cend(&ingredients); ++it) {
intptr_t ingredient_id = *it;
Set_t possible_allergens;
set_copy(&possible_allergens, &allergens);
for (size_t i = 0; i < food_count; ++i) {
if (set_contains(&(food[i].ingredients), ingredient_id)) {
Set_t new_possible_allergens = set_intersection(&possible_allergens, &(food[i].allergens));
set_free(&possible_allergens);
possible_allergens = new_possible_allergens;
if (possible_allergens.count == 0) {
break;
}
}
}
if (possible_allergens.count == 0) {
set_add(&allergen_free_ingredients, ingredient_id);
}
set_free(&possible_allergens);
}
printf("Ingredients that cannot contain allergens:\n");
set_print(&allergen_free_ingredients);
*/
Map_t ingredient_to_allergen_map;
map_init(&ingredient_to_allergen_map, allergens.count);
Map_t allergen_to_ingredient_map;
map_init(&allergen_to_ingredient_map, allergens.count);
int changes;
do {
changes = 0;
for (intptr_t const * it = set_cbegin(&allergens); it != set_cend(&allergens); ++it) {
intptr_t allergen_id = *it;
if (map_contains(&allergen_to_ingredient_map, allergen_id)) {
continue;
}
Set_t possible_ingredients;
set_copy(&possible_ingredients, &ingredients);
for (size_t i = 0; i < ingredient_to_allergen_map.count; ++i) {
set_remove(&possible_ingredients, ingredient_to_allergen_map.keys[i]);
}
for (size_t i = 0; i < food_count; ++i) {
if (set_contains(&(food[i].allergens), allergen_id)) {
Set_t new_possible_ingredients = set_intersection(&possible_ingredients, &(food[i].ingredients));
set_free(&possible_ingredients);
possible_ingredients = new_possible_ingredients;
ASSERT(possible_ingredients.count >= 1);
if (possible_ingredients.count == 1) {
break;
}
}
}
if (possible_ingredients.count == 1) {
map_set(&ingredient_to_allergen_map, possible_ingredients.values[0], allergen_id);
map_set(&allergen_to_ingredient_map, allergen_id, possible_ingredients.values[0]);
++changes;
}
set_free(&possible_ingredients);
}
} while (changes);
Set_t allergen_free_ingredients;
set_copy(&allergen_free_ingredients, &ingredients);
for (size_t i = 0; i < ingredient_to_allergen_map.count; ++i) {
set_remove(&allergen_free_ingredients, ingredient_to_allergen_map.keys[i]);
}
size_t answer = 0;
for (intptr_t const * it = set_cbegin(&allergen_free_ingredients); it != set_cend(&allergen_free_ingredients); ++it) {
for (size_t i = 0; i < food_count; ++i) {
if (set_contains(&(food[i].ingredients), *it)) {
++answer;
}
}
}
DISP(answer);
}