-
Notifications
You must be signed in to change notification settings - Fork 0
/
day21b.c
150 lines (125 loc) · 4.85 KB
/
day21b.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
#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);
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);
ASSERT(allergen_to_ingredient_map.count == allergens.count);
size_t order [allergens.count];
for (size_t i = 0; i < allergens.count; ++i) {
order[i] = i;
}
int swaps;
do {
swaps = 0;
for (size_t i = 1; i < allergens.count; ++i) {
char const * str0 = glossary_get_string(&glossary, allergens.values[order[i-1]]);
char const * str1 = glossary_get_string(&glossary, allergens.values[order[i]]);
if (strcmp(str0, str1) > 0) {
size_t tmp = order[i-1];
order[i-1] = order[i];
order[i] = tmp;
++swaps;
}
}
} while (swaps);
for (size_t i = 0; i < allergens.count; ++i) {
intptr_t allergen_id = allergens.values[order[i]];
intptr_t ingredient_id = map_get(&allergen_to_ingredient_map, allergen_id);
printf("%s%s", (i!=0)?",":"", glossary_get_string(&glossary, ingredient_id));
}
printf("\n");
}