-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14b.c
351 lines (282 loc) · 6.79 KB
/
day14b.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Licensed under the MIT License.
// Parabolic Reflector Dish Part 2
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define DICTIONARY_BUCKETS 193
#define DIMENSION 101
#define EXCEPTION_FORMAT "Error: Format.\n"
#define ITERATIONS 1000000000
enum
{
KEY_SIZE = (DIMENSION - 1) * (DIMENSION - 1)
};
struct DictionaryEntry
{
struct DictionaryEntry* nextEntry;
long value;
char key[KEY_SIZE];
};
struct DictionaryBucket
{
struct DictionaryEntry* firstEntry;
struct DictionaryBucket* nextBucket;
};
struct Dictionary
{
struct DictionaryBucket* firstBucket;
struct DictionaryBucket buckets[DICTIONARY_BUCKETS];
};
struct Matrix
{
int rows;
int columns;
char items[KEY_SIZE];
};
typedef char* Row;
typedef struct DictionaryEntry* DictionaryEntry;
typedef struct DictionaryBucket* DictionaryBucket;
typedef struct Dictionary* Dictionary;
typedef struct Matrix* Matrix;
void matrix(Matrix instance, int columns)
{
instance->rows = 0;
instance->columns = columns;
}
char matrix_get(Matrix instance, int i, int j)
{
return instance->items[(instance->columns * i) + j];
}
void matrix_set(Matrix instance, int i, int j, char value)
{
instance->items[(instance->columns * i) + j] = value;
}
void matrix_add_row(Matrix instance, char values[])
{
int m = instance->rows;
instance->rows = m + 1;
memcpy(
instance->items + (instance->columns * m),
values,
instance->columns);
}
void matrix_to_char_array(Matrix instance, char result[])
{
memcpy(result, instance->items, instance->rows * instance->columns);
}
bool dictionary_replace(
Dictionary instance,
char key[],
long* existingValue,
long newValue)
{
DictionaryEntry* p;
unsigned int hash = 7;
for (int i = 0; i < KEY_SIZE; i++)
{
hash = (hash * 31) + key[i];
}
hash %= DICTIONARY_BUCKETS;
for (p = &instance->buckets[hash].firstEntry; *p; p = &(*p)->nextEntry)
{
if (memcmp(key, (*p)->key, KEY_SIZE) == 0)
{
*existingValue = (*p)->value;
(*p)->value = newValue;
return true;
}
}
DictionaryEntry entry = malloc(sizeof * entry);
if (!entry)
{
return false;
}
if (!instance->buckets[hash].firstEntry)
{
DictionaryBucket first = instance->firstBucket;
instance->buckets[hash].nextBucket = first;
instance->firstBucket = instance->buckets + hash;
}
memcpy(entry->key, key, KEY_SIZE);
entry->value = newValue;
entry->nextEntry = NULL;
*p = entry;
return true;
}
void dictionary_clear(Dictionary instance)
{
for (DictionaryBucket bucket = instance->firstBucket;
bucket;
bucket = bucket->nextBucket)
{
DictionaryEntry entry = bucket->firstEntry;
while (entry)
{
DictionaryEntry nextEntry = entry->nextEntry;
free(entry);
entry = nextEntry;
}
bucket->firstEntry = NULL;
}
instance->firstBucket = NULL;
}
static long scan(Matrix matrix)
{
long result = 0;
for (int i = 0; i < matrix->rows; i++)
{
for (int j = 0; j < matrix->columns; j++)
{
if (matrix_get(matrix, i, j) == 'O')
{
result += matrix->rows - i;
}
}
}
return result;
}
static void roll_hi(Matrix matrix)
{
for (int i = 0; i < matrix->rows; i++)
{
for (int j = 0; j < matrix->columns; j++)
{
if (matrix_get(matrix, i, j) != 'O')
{
continue;
}
for (int k = i - 1; k >= 0 && matrix_get(matrix, k, j) == '.'; k--)
{
matrix_set(matrix, k, j, 'O');
matrix_set(matrix, k + 1, j, '.');
}
}
}
}
static void roll_lo(Matrix matrix)
{
for (int i = matrix->rows - 1; i >= 0; i--)
{
for (int j = 0; j < matrix->columns; j++)
{
if (matrix_get(matrix, i, j) != 'O')
{
continue;
}
for (int k = i + 1;
k < matrix->rows && matrix_get(matrix, k, j) == '.';
k++)
{
matrix_set(matrix, k, j, 'O');
matrix_set(matrix, k - 1, j, '.');
}
}
}
}
static void roll_left(Matrix matrix)
{
for (int i = 0; i < matrix->rows; i++)
{
for (int j = 0; j < matrix->columns; j++)
{
if (matrix_get(matrix, i, j) != 'O')
{
continue;
}
for (int k = j - 1; k >= 0 && matrix_get(matrix, i, k) == '.'; k--)
{
matrix_set(matrix, i, k, 'O');
matrix_set(matrix, i, k + 1, '.');
}
}
}
}
static void roll_right(Matrix matrix)
{
for (int i = matrix->rows - 1; i >= 0; i--)
{
for (int j = matrix->columns - 1; j >= 0; j--)
{
if (matrix_get(matrix, i, j) != 'O')
{
continue;
}
for (int k = j + 1;
k < matrix->columns && matrix_get(matrix, i, k) == '.';
k++)
{
matrix_set(matrix, i, k, 'O');
matrix_set(matrix, i, k - 1, '.');
}
}
}
}
static void roll(Matrix matrix)
{
roll_hi(matrix);
roll_left(matrix);
roll_lo(matrix);
roll_right(matrix);
}
static bool roll_many(Matrix matrix, Dictionary cache)
{
long i = 0;
while (i < ITERATIONS)
{
long previous = -1;
char key[KEY_SIZE];
roll(matrix);
matrix_to_char_array(matrix, key);
if (!dictionary_replace(cache, key, &previous, i))
{
return false;
}
if (previous != -1)
{
i = ITERATIONS - ((ITERATIONS - previous) % (i - previous)) + 1;
break;
}
i++;
}
while (i < ITERATIONS)
{
roll(matrix);
i++;
}
return true;
}
int main(void)
{
char buffer[DIMENSION + 1];
clock_t start = clock();
if (!fgets(buffer, sizeof buffer, stdin))
{
fprintf(stderr, EXCEPTION_FORMAT);
return 1;
}
int n = strlen(buffer) - 1;
if (n < 1)
{
fprintf(stderr, EXCEPTION_FORMAT);
return 1;
}
struct Matrix a;
struct Dictionary cache = { 0 };
matrix(&a, n);
do
{
matrix_add_row(&a, buffer);
}
while (fgets(buffer, n + 2, stdin));
if (!roll_many(&a, &cache))
{
fprintf(stderr, "Error: Out of memory.\n");
return 1;
}
long total = scan(&a);
printf("14b %ld %lf\n", total, (double)(clock() - start) / CLOCKS_PER_SEC);
dictionary_clear(&cache);
return 0;
}