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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#define QUEUE_CAP 128
#define CHILDREN_CAP 128
#define ARR_SIZE(a) (sizeof(a)/sizeof(*a))
// #define FILENAME "sample.txt"
#define FILENAME "input.txt"
// #define PART1
#define PART2
#define NUMS(X) \
X(1, 1, 177622) \
X(2, 2, 177623) \
X(3, 3, 177624) \
X(4, 4, 177625) \
X(5, 5, 177626) \
X(6, 6, 177627) \
X(7, 7, 177628) \
X(8, 8, 177629) \
X(9, 9, 177630) \
#define STRS(X) \
X(one, 1, 193501607) \
X(two, 2, 193507359) \
X(three, 3, 210728981597) \
X(four, 4, 6385231329) \
X(five, 5, 6385224815) \
X(six, 6, 193505817) \
X(seven, 7, 210727692230) \
X(eight, 8, 210711216854) \
X(nine, 9, 6385512047) \
#define TO_STR_ARR(s, v, h) #s,
#define TO_CASE(s, v, h) \
case h: \
if(num_str[0] == 0) { \
num_str[0] = #v[0]; \
num_str[1] = #v[0]; \
} else { \
num_str[1] = #v[0]; \
} \
break;
// https://www.cse.yorku.ca/~oz/hash.html
unsigned long hash(char *str)
{
unsigned long hash = 5381;
int c;
while ((c = *str++))
hash = ((hash << 5) + hash) + c;
return hash;
}
// https://en.wikipedia.org/wiki/Aho%E2%80%93Corasick_algorithm
typedef struct trie_node {
struct trie_node *parent;
struct trie_node *children[CHILDREN_CAP];
struct trie_node *suffix;
bool end;
} trie;
trie *trie_init(char **dictionary, size_t nwords);
void trie_search(trie *root, char *str, void (*callback)(char *str, size_t len));
void trie_free(trie *root);
void trie_print(trie *root);
size_t sum = 0;
char num_str[3] = {0};
void handle_match(char *str, size_t len)
{
char s[len+1]; s[len] = '\0';
memcpy(s, str, len);
switch(hash(s)) {
NUMS(TO_CASE)
#ifdef PART2
STRS(TO_CASE)
#endif
default:
fprintf(stderr, "bad hash: %lu (of str %s)", hash(s), s);
}
}
int main(void)
{
char *dictionary[] = {
NUMS(TO_STR_ARR)
#ifdef PART2
STRS(TO_STR_ARR)
#endif
};
trie *root = trie_init(dictionary, ARR_SIZE(dictionary));
trie_print(root);
FILE *fp = fopen(FILENAME, "r");
if(!fp) {
perror("fopen: failed");
return 1;
}
char line[256];
while(fgets(line, sizeof(line), fp)) {
trie_search(root, line, handle_match);
sum += atoi(num_str);
num_str[0] = 0; num_str[1] = 0;
}
fclose(fp);
printf("Sum: %ld\n", sum);
trie_free(root);
return 0;
}
static trie *trie_find_suffix(trie *node, size_t idx);
static void trie_set_suffix(trie *node, size_t idx);
static void trie_traverse(trie *parent, void (*callback)(trie *node, size_t idx));
static void trie_add_pattern(trie *node, char *pattern);
trie *trie_init(char **patterns, size_t nwords)
{
trie *root = malloc(sizeof(trie));
memset(root, 0, sizeof(trie));
// generate the basic trie
for(size_t i = 0; i < nwords; i++) {
trie_add_pattern(root, patterns[i]);
}
// calculate suffix links
trie_traverse(root, trie_set_suffix);
return root;
}
void trie_search(trie *root, char *str, void (*callback)(char *str, size_t len))
{
trie *node = root;
for(size_t i = 0; i < strlen(str)+1; i++) {
if(node->children[(size_t)str[i]] != NULL) {
node = node->children[(size_t)str[i]];
continue;
}
if(node->end) {
int len = 0; trie *tmp = node;
while(tmp->parent != NULL) {
tmp = tmp->parent;
len++;
}
callback(&str[i-len], len);
}
if(node->suffix == NULL) continue;
node = node->suffix;
i--; continue;
}
}
void trie_free(trie *root)
{
for(int i = 0; i < CHILDREN_CAP; i++)
if(root->children[i] != NULL) {
trie_free(root->children[i]);
}
free(root);
}
static void trie_print_child(trie *root, trie *node, size_t depth)
{
for(int i = 0; i < CHILDREN_CAP; i++) {
if(node->children[i] != NULL) {
for(size_t i = 0; i < depth; i++) printf(" ");
printf("%c (%p) -> (%p) %s\n", i, (void *)node->children[i],
(void *)((node->children[i]->suffix == root) ?
0 : node->children[i]->suffix),
(node->children[i]->end ? "END" : ""));
trie_print_child(root, node->children[i], depth+2);
}
}
}
void trie_print(trie *root)
{
printf("ROOT (%p)\n", (void *)root);
trie_print_child(root, root, 2);
}
static trie *trie_find_suffix(trie *node, size_t idx)
{
if(node->suffix == NULL) return node; // root
if(node->suffix->children[idx] != NULL) {
return node->suffix->children[idx];
} else {
return trie_find_suffix(node->suffix, idx);
}
}
static void trie_set_suffix(trie *node, size_t idx)
{
node->suffix = trie_find_suffix(node->parent, idx);
}
static void trie_traverse(trie *root, void (*callback)(trie *node, size_t idx))
{
// breadth-first traversal
trie *queue[QUEUE_CAP] = {0};
size_t queue_start = 0, queue_end = 0;
queue[queue_end++] = root;
while(queue_start != queue_end) {
trie *parent = queue[queue_start++ % QUEUE_CAP];
for(size_t i = 0; i < CHILDREN_CAP; i++)
if(parent->children[i] != NULL) {
callback(parent->children[i], i);
queue[queue_end++ % QUEUE_CAP] = parent->children[i];
}
}
}
static void trie_add_pattern(trie *node, char *pattern)
{
if(pattern[0] == '\0') {
node->end = true;
return;
}
if(node->children[(size_t)pattern[0]] == NULL) {
trie *child = malloc(sizeof(trie));
memset(child, 0, sizeof(trie));
child->parent = node;
node->children[(size_t)pattern[0]] = child;
}
trie_add_pattern(node->children[(size_t)pattern[0]], &pattern[1]);
}
|