day 25 solved in C
[aoc_eblake.git] / 2017 / advent13.c
blob0ea33f64598afe52e8cd73b5309d95caebf2891a
1 #define _GNU_SOURCE 1
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <stdbool.h>
8 int main(void)
10 int severity = 0;
11 int count = 0;
12 int layer;
13 int depth;
14 int list[100] = {0}; // cheat based on pre-inspecting input
15 int max = 0;
16 while (scanf("%d: %d\n", &layer, &depth) == 2) {
17 count++;
18 max = layer;
19 list[layer] = depth;
21 printf ("read %d items, max layer %d\n", count, max);
22 int delay = -1;
23 while (1) {
24 delay++;
25 int i;
26 for (i = 0; i <= max; i++) {
27 if (!list[i])
28 continue;
29 int cycle = (list[i] - 1) * 2;
30 if (! ((i + delay) % cycle)) {
31 // printf (" delay %d, i %d, cycle %d\n", delay, i, cycle);
32 if (!delay)
33 severity += i * list[i];
34 else
35 break;
38 if (delay && i > max)
39 break;
41 printf ("with no delay, severity is %d\n", severity);
42 printf ("delay %d to avoid collisions\n", delay);
43 return 0;