10 bool debug(const char *fmt
, ...) {
12 if (getenv("DEBUG")) {
14 vfprintf(stderr
, fmt
, ap
);
21 #define LIMIT 25000000
23 static char list
[LIMIT
] = "37";
25 static void dump(int iter
, int size
, int idx1
, int idx2
) {
26 if (!debug("iter %d:", iter
))
28 for (int i
= 0; i
< size
; i
++)
30 debug("(%c)", list
[i
]);
32 debug("[%c]", list
[i
]);
34 debug(" %c", list
[i
]);
38 int main(int argc
, char **argv
) {
39 const char *goal
= "077201";
52 if (fread(buf
, 1, 6, f
) != 6) {
53 fprintf(stderr
, "error reading file %s\n", goal
);
62 fprintf(stderr
, "recompile with larger limit\n");
66 while (!strstr(list
+ size
- 10, goal
) && size
< max
- 1) {
68 int e
= list
[idx1
] - '0' + list
[idx2
] - '0';
73 list
[size
++] = e
+ '0';
74 idx1
= (list
[idx1
] - '0' + idx1
+ 1) % size
;
75 idx2
= (list
[idx2
] - '0' + idx2
+ 1) % size
;
76 if (!(size
& 0xffffe))
77 printf("%d...\n", size
);
79 dump(iter
, size
, idx1
, idx2
);
80 char *p
= strstr(list
+ size
- 10, goal
);
82 printf("%td recipes before goal %s (%d iterations)\n",
83 p
- list
, goal
, iter
);
85 printf("%d iterations and size %d insufficient to find goal %s\n",