10 bool debug(const char *fmt
, ...) {
12 if (getenv("DEBUG")) {
14 vfprintf(stderr
, fmt
, ap
);
23 #define PACK(a, b) (((a)<<4)|(b))
25 static uint8_t array
[LIMIT
] = {
45 static unsigned need
; /* iterations needed for part 1 */
46 static char part1
[11] = "unknown";
47 static unsigned pattern
; /* goal for part 2 */
49 static unsigned recipes
; /* recipes processed so far */
50 static uint64_t recent
; /* fifo of most-recently-seen hex digits */
51 static unsigned next
; /* next array index awaiting assignment */
52 static uint8_t remain
; /* recipes to skip before assigning next */
54 static bool e(uint8_t value
) {
56 return e(1) || e(value
- 10);
57 if (recipes
++ == need
)
58 sprintf(part1
, "%010llx", recent
& 0xffffffffffULL
);
63 array
[next
++] = PACK(remain
= value
, 1);
64 recent
= (recent
<< 4) | value
;
65 return (recent
& mask
) == pattern
;
68 int main(int argc
, char **argv
) {
69 const char *goal
= "077201";
81 if (fread(buf
, 1, 6, f
) != 6) {
82 fprintf(stderr
, "error reading file %s\n", goal
);
88 need
= strtol(goal
, NULL
, 10) + 10;
90 pattern
= strtol(goal
, NULL
, 16);
91 mask
= (1 << 4*len
) - 1;
96 recent
= 0x1677925107ULL
;
105 val
= (array
[idx1
] + array
[idx2
]) >> 4;
106 idx1
+= NEXT(array
[idx1
]);
107 idx2
+= NEXT(array
[idx2
]);
110 printf("after %s recipes, next ten recipes are %s\n", goal
, part1
);
111 printf("%s found after %d recipes (%d iterations, array len %d)\n", goal
,
112 recipes
- len
, iter
, next
);