-
Notifications
You must be signed in to change notification settings - Fork 0
/
day20b.c
714 lines (570 loc) · 14.6 KB
/
day20b.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
// Licensed under the MIT License.
// Pulse Propagation Part 2
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define BUFFER_SIZE 32
#define DICTIONARY_BUCKETS 53
#define DELIMITERS ", \n"
#define EXCEPTION_FORMAT "Error: Format.\n"
#define EXCEPTION_OUT_OF_MEMORY "Error: Out of memory.\n"
#define MESSAGE_QUEUE_CAPACITY 64
#define MODULE_COLLECTION_BUCKETS 97
#define MODULE_TARGETS_CAPACITY 8
#define SET_BUCKETS 53
#define STRING_CAPACITY 16
enum AddResult
{
ADD_RESULT_ADDED,
ADD_RESULT_NOT_ADDED,
ADD_RESULT_OUT_OF_MEMORY
};
struct String
{
int length;
char buffer[STRING_CAPACITY];
};
struct DictionaryEntry
{
struct DictionaryEntry* nextEntry;
struct String key;
bool value;
};
struct DictionaryBucket
{
struct DictionaryEntry* firstEntry;
struct DictionaryBucket* nextBucket;
};
struct Dictionary
{
struct DictionaryBucket* firstBucket;
struct DictionaryBucket buckets[DICTIONARY_BUCKETS];
};
struct Message
{
struct String source;
struct String target;
bool pulse;
};
struct MessageQueue
{
int first;
int last;
struct Message items[MESSAGE_QUEUE_CAPACITY];
};
union ModuleType
{
bool pulse;
struct Dictionary* pulses;
};
struct Module
{
struct Module* nextModule;
union ModuleType child;
int targetCount;
struct String name;
struct String targets[MODULE_TARGETS_CAPACITY];
bool isConjunction;
};
struct ModuleCollectionBucket
{
struct Module* firstModule;
struct ModuleCollectionBucket* nextBucket;
};
struct ModuleCollection
{
struct ModuleCollectionBucket* firstBucket;
struct ModuleCollectionBucket buckets[MODULE_COLLECTION_BUCKETS];
};
struct SetEntry
{
struct SetEntry* nextEntry;
struct String item;
};
struct SetBucket
{
struct SetEntry* firstEntry;
struct SetBucket* nextBucket;
};
struct Set
{
struct SetBucket* firstBucket;
struct SetBucket buckets[DICTIONARY_BUCKETS];
int count;
};
typedef char* ZString;
typedef enum AddResult AddResult;
typedef struct String* String;
typedef struct DictionaryEntry* DictionaryEntry;
typedef struct DictionaryBucket* DictionaryBucket;
typedef struct Dictionary* Dictionary;
typedef struct Message* Message;
typedef struct MessageQueue* MessageQueue;
typedef struct Module* Module;
typedef struct ModuleCollectionBucket* ModuleCollectionBucket;
typedef struct ModuleCollection* ModuleCollection;
typedef struct SetEntry* SetEntry;
typedef struct SetBucket* SetBucket;
typedef struct Set* Set;
long long math_gcd(long long a, long long b)
{
while (b > 0)
{
long long r = a % b;
a = b;
b = r;
}
return a;
}
long long math_lcm(long long a, long long b)
{
return (a / math_gcd(a, b)) * b;
}
void string(String instance, ZString buffer)
{
instance->length = strlen(buffer);
memcpy(instance->buffer, buffer, 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;
}
unsigned int string_get_hash_code(String instance)
{
unsigned int hash = 0;
for (int i = 0; i < instance->length; i++)
{
hash = (hash * 33) + instance->buffer[i];
}
return hash;
}
void dictionary(Dictionary instance)
{
instance->firstBucket = NULL;
for (int i = 0; i < DICTIONARY_BUCKETS; i++)
{
instance->buckets[i].firstEntry = NULL;
instance->buckets[i].nextBucket = NULL;
}
}
bool dictionary_try_get_value(Dictionary instance, String key, bool* result)
{
unsigned int hash = string_get_hash_code(key) % DICTIONARY_BUCKETS;
for (DictionaryEntry entry = instance->buckets[hash].firstEntry;
entry;
entry = entry->nextEntry)
{
if (string_equals(key, &entry->key))
{
*result = entry->value;
return true;
}
}
return false;
}
bool dictionary_set(Dictionary instance, String key, int value)
{
DictionaryEntry* p;
unsigned int hash = string_get_hash_code(key) % DICTIONARY_BUCKETS;
for (p = &instance->buckets[hash].firstEntry; *p; p = &(*p)->nextEntry)
{
if (string_equals(key, &(*p)->key))
{
(*p)->value = value;
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;
}
string_copy(&entry->key, key);
entry->value = value;
entry->nextEntry = NULL;
*p = entry;
return true;
}
bool dictionary_all(Dictionary instance)
{
for (DictionaryBucket bucket = instance->firstBucket;
bucket;
bucket = bucket->nextBucket)
{
for (DictionaryEntry entry = bucket->firstEntry;
entry;
entry = entry->nextEntry)
{
if (!entry->value)
{
return false;
}
}
}
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;
}
void message_queue(MessageQueue instance)
{
instance->first = -1;
instance->last = -1;
}
Message message_queue_enqueue(MessageQueue instance)
{
if (instance->first == -1)
{
instance->first = 0;
instance->last = 0;
}
else if (instance->first && instance->last == MESSAGE_QUEUE_CAPACITY - 1)
{
instance->last = 0;
}
else
{
instance->last++;
}
return instance->items + instance->last;
}
bool message_queue_try_dequeue(MessageQueue instance, Message result)
{
if (instance->first == -1)
{
return false;
}
*result = instance->items[instance->first];
if (instance->first == instance->last)
{
instance->first = -1;
instance->last = -1;
}
else if (instance->first == MESSAGE_QUEUE_CAPACITY - 1)
{
instance->first = 0;
}
else
{
instance->first++;
}
return true;
}
bool module(Module instance, bool isConjunction, ZString name)
{
instance->isConjunction = isConjunction;
instance->nextModule = NULL;
instance->targetCount = 0;
if (isConjunction)
{
instance->child.pulses = malloc(sizeof * instance->child.pulses);
if (!instance->child.pulses)
{
return false;
}
dictionary(instance->child.pulses);
}
else
{
instance->child.pulse = false;
}
string(&instance->name, name);
return true;
}
String module_new_target(Module instance)
{
String result = instance->targets + instance->targetCount;
instance->targetCount++;
return result;
}
void module_send(Module module, MessageQueue queue, bool pulse)
{
for (int i = 0; i < module->targetCount; i++)
{
Message message = message_queue_enqueue(queue);
message->pulse = pulse;
string_copy(&message->source, &module->name);
string_copy(&message->target, module->targets + i);
}
}
bool module_respond(Module instance, Message message, MessageQueue queue)
{
if (instance->isConjunction)
{
if (!dictionary_set(
instance->child.pulses,
&message->source,
message->pulse))
{
fprintf(stderr, EXCEPTION_OUT_OF_MEMORY);
return false;
}
module_send(instance, queue, !dictionary_all(instance->child.pulses));
}
else
{
if (message->pulse)
{
return true;
}
instance->child.pulse = !instance->child.pulse;
module_send(instance, queue, instance->child.pulse);
}
return true;
}
Module module_collection_get(ModuleCollection instance, String name)
{
unsigned int hash = string_get_hash_code(name) % MODULE_COLLECTION_BUCKETS;
for (Module module = instance->buckets[hash].firstModule;
module;
module = module->nextModule)
{
if (string_equals(name, &module->name))
{
return module;
}
}
return NULL;
}
void finalize_module(Module instance)
{
if (instance->isConjunction)
{
dictionary_clear(instance->child.pulses);
free(instance->child.pulses);
}
}
void module_collection_add(ModuleCollection instance, Module item)
{
unsigned int hash = string_get_hash_code(&item->name) %
MODULE_COLLECTION_BUCKETS;
if (!instance->buckets[hash].firstModule)
{
ModuleCollectionBucket first = instance->firstBucket;
instance->buckets[hash].nextBucket = first;
instance->firstBucket = instance->buckets + hash;
}
item->nextModule = instance->buckets[hash].firstModule;
instance->buckets[hash].firstModule = item;
}
AddResult set_add(Set instance, String item)
{
SetEntry* p;
unsigned int hash = string_get_hash_code(item) % SET_BUCKETS;
for (p = &instance->buckets[hash].firstEntry; *p; p = &(*p)->nextEntry)
{
if (string_equals(item, &(*p)->item))
{
return ADD_RESULT_NOT_ADDED;
}
}
SetEntry entry = malloc(sizeof * entry);
if (!entry)
{
return ADD_RESULT_OUT_OF_MEMORY;
}
if (!instance->buckets[hash].firstEntry)
{
SetBucket first = instance->firstBucket;
instance->buckets[hash].nextBucket = first;
instance->firstBucket = instance->buckets + hash;
}
string_copy(&entry->item, item);
entry->nextEntry = NULL;
*p = entry;
instance->count++;
return ADD_RESULT_ADDED;
}
void set_clear(Set instance)
{
for (SetBucket bucket = instance->firstBucket;
bucket;
bucket = bucket->nextBucket)
{
SetEntry entry = bucket->firstEntry;
while (entry)
{
SetEntry nextEntry = entry->nextEntry;
free(entry);
entry = nextEntry;
}
bucket->firstEntry = NULL;
}
instance->firstBucket = NULL;
instance->count = 0;
}
static bool scan(
ModuleCollection modules,
Set visited,
Module broadcaster,
Module sender,
long long* result)
{
*result = 1;
int iterations = 0;
while (visited->count < 4)
{
iterations++;
struct Message current;
struct MessageQueue queue;
message_queue(&queue);
module_send(broadcaster, &queue, false);
while (message_queue_try_dequeue(&queue, ¤t))
{
Module target = module_collection_get(modules, ¤t.target);
if (!target)
{
continue;
}
if (target == sender && current.pulse)
{
AddResult status = set_add(visited, ¤t.source);
if (status == ADD_RESULT_OUT_OF_MEMORY)
{
return false;
}
*result = math_lcm(*result, iterations);
}
if (!module_respond(target, ¤t, &queue))
{
return false;
}
}
}
return true;
}
int main(void)
{
clock_t start = clock();
char buffer[BUFFER_SIZE];
struct ModuleCollection modules = { 0 };
struct String receiver =
{
.length = 2,
.buffer = { 'r', 'x' }
};
Module sender = NULL;
while (fgets(buffer, sizeof buffer, stdin))
{
char* mid = strstr(buffer, " -> ");
if (!mid)
{
fprintf(stderr, EXCEPTION_FORMAT);
return 1;
}
*mid = '\0';
Module next = malloc(sizeof * next);
if (!next || !module(next, buffer[0] != '%', buffer + 1))
{
fprintf(stderr, EXCEPTION_OUT_OF_MEMORY);
return 1;
}
module_collection_add(&modules, next);
for (ZString token = strtok(mid + 4, DELIMITERS);
token;
token = strtok(NULL, DELIMITERS))
{
String target = module_new_target(next);
string(target, token);
if (string_equals(target, &receiver))
{
sender = next;
}
}
}
if (!sender)
{
fprintf(stderr, EXCEPTION_FORMAT);
return 1;
}
for (ModuleCollectionBucket bucket = modules.firstBucket;
bucket;
bucket = bucket->nextBucket)
{
for (Module module = bucket->firstModule;
module;
module = module->nextModule)
{
for (int i = 0; i < module->targetCount; i++)
{
Module target = module_collection_get(
&modules,
module->targets + i);
if (!target || !target->isConjunction)
{
continue;
}
if (!dictionary_set(target->child.pulses, &module->name, false))
{
fprintf(stderr, EXCEPTION_OUT_OF_MEMORY);
return 1;
}
}
}
}
struct String broadcasterKey =
{
.length = 10,
.buffer = { 'r', 'o', 'a', 'd', 'c', 'a', 's', 't', 'e', 'r' }
};
Module broadcaster = module_collection_get(&modules, &broadcasterKey);
if (!broadcaster)
{
fprintf(stderr, "Error: Key not found.\n");
return 1;
}
struct Set visited = { 0 };
long long lcm;
if (!scan(&modules, &visited, broadcaster, sender, &lcm))
{
fprintf(stderr, EXCEPTION_OUT_OF_MEMORY);
return 1;
}
printf("20b %lld %lf\n", lcm, (double)(clock() - start) / CLOCKS_PER_SEC);
set_clear(&visited);
for (ModuleCollectionBucket bucket = modules.firstBucket;
bucket;
bucket = bucket->nextBucket)
{
Module module = bucket->firstModule;
while (module)
{
Module next = module->nextModule;
finalize_module(module);
free(module);
module = next;
}
}
return 0;
}