day 22 part 1
[aoc_eblake.git] / 2018 / day14a.c
blob946f4aaa19925cfc3e8b644e2ea728d4b84713d1
1 #define _GNU_SOURCE 1
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <stdarg.h>
6 #include <stdbool.h>
7 #include <inttypes.h>
8 #include <assert.h>
10 bool debug(const char *fmt, ...) {
11 va_list ap;
12 if (getenv("DEBUG")) {
13 va_start(ap, fmt);
14 vfprintf(stderr, fmt, ap);
15 va_end(ap);
16 return true;
18 return false;
21 #define LIMIT 200000
23 static char list[LIMIT] = "37";
25 static void dump(int iter, int size, int idx1, int idx2) {
26 if (!debug("iter %d:", iter))
27 return;
28 for (int i = 0; i < size; i++)
29 if (i == idx1)
30 debug("(%c)", list[i]);
31 else if (i == idx2)
32 debug("[%c]", list[i]);
33 else
34 debug(" %c", list[i]);
35 debug("\n");
38 int main(int argc, char **argv) {
39 int goal = 77201;
40 int max;
41 int size = 2;
42 int idx1 = 0;
43 int idx2 = 1;
44 int iter = 0;
46 if (argc > 1)
47 goal = atoi(argv[1]);
48 if (argc > 2)
49 max = atoi(argv[2]);
50 else
51 max = goal + 12;
52 if (goal > max - 11) {
53 fprintf(stderr, "recompile with larger limit to hit goal of %d\n", goal);
54 exit(1);
57 while (size < max) {
58 dump(iter++, size, idx1, idx2);
59 size += sprintf(list + size, "%d", list[idx1] - '0' + list[idx2] - '0');
60 idx1 = (list[idx1] - '0' + idx1 + 1) % size;
61 idx2 = (list[idx2] - '0' + idx2 + 1) % size;
62 if (size % 1000 < 2)
63 printf("%d...\n", size);
65 dump(iter, size, idx1, idx2);
66 printf("score: %.10s\n", list + goal);
67 return 0;