-
Notifications
You must be signed in to change notification settings - Fork 0
/
day15b.c
281 lines (224 loc) · 6.49 KB
/
day15b.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
// Licensed under the MIT license.
// Lens Library Part 2
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define EXCEPTION_OUT_OF_MEMORY "Error: Out of memory.\n"
#define ORDERED_DICTIONARY_BUCKETS 256
#define STRING_CAPACITY 8
struct String
{
int length;
char buffer[STRING_CAPACITY];
};
struct OrderedDictionaryEntry
{
struct OrderedDictionaryEntry* nextEntry;
int value;
struct String key;
};
struct OrderedDictionaryBucket
{
struct OrderedDictionaryEntry* firstEntry;
struct OrderedDictionaryBucket* nextBucket;
struct OrderedDictionaryBucket* previousBucket;
};
struct OrderedDictionary
{
struct OrderedDictionaryBucket* firstBucket;
struct OrderedDictionaryBucket buckets[ORDERED_DICTIONARY_BUCKETS];
};
typedef struct String* String;
typedef struct OrderedDictionaryEntry* OrderedDictionaryEntry;
typedef struct OrderedDictionaryBucket* OrderedDictionaryBucket;
typedef struct OrderedDictionary* OrderedDictionary;
void string(String instance)
{
instance->length = 0;
}
void string_append(String instance, char value)
{
instance->buffer[instance->length] = value;
instance->length++;
}
void string_copy(String destination, String source)
{
destination->length = source->length;
memcpy(destination->buffer, source->buffer, source->length);
}
bool string_equals(String instance, String other)
{
if (instance->length != other->length)
{
return false;
}
return memcmp(instance->buffer, other->buffer, instance->length) == 0;
}
bool ordered_dictionary_set(
OrderedDictionary instance,
String key,
int hash,
int value)
{
OrderedDictionaryEntry* p;
for (p = &instance->buckets[hash].firstEntry; *p; p = &(*p)->nextEntry)
{
if (string_equals(key, &(*p)->key))
{
(*p)->value = value;
return true;
}
}
OrderedDictionaryEntry entry = malloc(sizeof * entry);
if (!entry)
{
return false;
}
if (!instance->buckets[hash].firstEntry)
{
OrderedDictionaryBucket first = instance->firstBucket;
instance->buckets[hash].nextBucket = first;
instance->firstBucket = instance->buckets + hash;
if (first)
{
first->previousBucket = instance->buckets + hash;
}
}
string_copy(&entry->key, key);
entry->value = value;
entry->nextEntry = NULL;
*p = entry;
return true;
}
void ordered_dictionary_remove(OrderedDictionary instance, String key, int hash)
{
OrderedDictionaryEntry previous = NULL;
for (OrderedDictionaryEntry current = instance->buckets[hash].firstEntry;
current;
current = current->nextEntry)
{
if (string_equals(key, ¤t->key))
{
if (previous)
{
previous->nextEntry = current->nextEntry;
}
else if (current->nextEntry)
{
instance->buckets[hash].firstEntry = current->nextEntry;
}
else
{
if (instance->firstBucket == instance->buckets + hash)
{
instance->firstBucket = instance->buckets[hash].nextBucket;
}
if (instance->buckets[hash].nextBucket)
{
instance->buckets[hash].nextBucket->previousBucket =
instance->buckets[hash].previousBucket;
}
if (instance->buckets[hash].previousBucket)
{
instance->buckets[hash].previousBucket->nextBucket =
instance->buckets[hash].nextBucket;
}
instance->buckets[hash].firstEntry = NULL;
}
free(current);
break;
}
previous = current;
}
}
void ordered_dictionary_clear(OrderedDictionary instance)
{
for (OrderedDictionaryBucket bucket = instance->firstBucket;
bucket;
bucket = bucket->nextBucket)
{
OrderedDictionaryEntry entry = bucket->firstEntry;
while (entry)
{
OrderedDictionaryEntry nextEntry = entry->nextEntry;
free(entry);
entry = nextEntry;
}
bucket->firstEntry = NULL;
}
instance->firstBucket = NULL;
}
int main(void)
{
int hash = 0;
int value = 0;
char current;
struct String key;
struct OrderedDictionary dictionary = { 0 };
clock_t start = clock();
string(&key);
while ((current = getc(stdin)) != EOF)
{
if (isdigit(current))
{
value = (value * 10) + (current - '0');
continue;
}
switch (current)
{
case '\n':
case '=':
continue;
case '-':
{
ordered_dictionary_remove(&dictionary, &key, hash);
}
continue;
case ',':
{
if (value)
{
if (!ordered_dictionary_set(&dictionary, &key, hash, value))
{
fprintf(stderr, EXCEPTION_OUT_OF_MEMORY);
return 1;
}
}
hash = 0;
value = 0;
string(&key);
}
continue;
}
hash = ((hash + current) * 17) % ORDERED_DICTIONARY_BUCKETS;
string_append(&key, current);
}
if (value)
{
if (!ordered_dictionary_set(&dictionary, &key, hash, value))
{
fprintf(stderr, EXCEPTION_OUT_OF_MEMORY);
return 1;
}
}
long sum = 0;
for (OrderedDictionaryBucket bucket = dictionary.firstBucket;
bucket;
bucket = bucket->nextBucket)
{
int slot = 1;
for (OrderedDictionaryEntry entry = bucket->firstEntry;
entry;
entry = entry->nextEntry)
{
sum += (bucket - dictionary.buckets + 1) * slot * entry->value;
slot++;
}
}
printf("15b %ld %lf\n", sum, (double)(clock() - start) / CLOCKS_PER_SEC);
ordered_dictionary_clear(&dictionary);
return 0;
}