10 void debug(const char *fmt
, ...) {
12 if (getenv("DEBUG")) {
14 vfprintf(stderr
, fmt
, ap
);
19 typedef struct marble marble
;
26 static void dump1(marble
*c
) {
27 debug("%d,%d,%d\n", c
->l
->value
, c
->value
, c
->r
->value
);
30 static void dump(int i
, marble
*c
) {
33 debug(" Iteration %d:\n", i
);
41 int main(int argc
, char **argv
) {
51 players
= atoi(argv
[1]);
53 marbles
= atoi(argv
[2]);
55 current
= calloc(1, sizeof *current
);
56 current
->l
= current
->r
= current
;
57 list
= calloc(players
, sizeof *list
);
61 for (i
= 1; i
<= marbles
; dump(i
++, current
)) {
63 tmp
= calloc(1, sizeof *tmp
);
67 tmp
->r
= current
->r
->r
;
68 current
= current
->r
->r
= tmp
->r
->l
= tmp
;
70 tmp
= current
->l
->l
->l
->l
->l
->l
->l
;
71 current
= tmp
->l
->r
= tmp
->r
;
73 list
[i
% players
] += i
+ tmp
->value
;
78 for (i
= 0; i
< players
; i
++)
81 printf("best: %" PRIu64
"\n", best
);