8 void debug(const char *fmt
, ...) {
10 if (getenv("DEBUG")) {
12 vfprintf(stderr
, fmt
, ap
);
19 short array
[LIMIT
][60];
22 static void process(int guard
, int start
, int end
) {
23 debug("processing %d: %d-%d\n", guard
, start
, end
);
24 for (int i
= start
; i
< end
; i
++)
28 int main(int argc
, char **argv
) {
29 size_t len
= 0, count
= 0;
38 if (!(stdin
= freopen(argv
[1], "r", stdin
))) {
43 while (getline(&line
, &len
, stdin
) >= 0) {
46 p
= strchr(line
, ':');
48 fprintf(stderr
, "bad input\n");
51 wake
= (p
[1] - '0') * 10 + p
[2] - '0';
52 process(guard
, sleep
, wake
);
55 p
= strchr(line
, '#');
57 if (sscanf(p
, "#%d ", &guard
) != 1) {
58 fprintf(stderr
, "bad input\n");
62 printf("recompile with larger LIMIT!\n");
67 p
= strchr(line
, ':');
69 fprintf(stderr
, "bad input\n");
72 sleep
= (p
[1] - '0') * 10 + p
[2] - '0';
77 printf("read %zd lines\n", count
);
81 for (i
= 0; i
< LIMIT
; i
++) {
86 for (j
= 0; j
< 60; j
++) {
87 local_sleep
+= array
[i
][j
];
88 if (array
[i
][j
] > array
[i
][local_minute
])
91 debug("guard %d slept %d best minute %d: %d\n", i
, local_sleep
,
92 local_minute
, array
[i
][local_minute
]);
93 if (array
[i
][local_minute
] > total_sleep
) {
94 total_sleep
= array
[i
][local_minute
];
96 best_minute
= local_minute
;
99 printf("best guard %d at minute %d = %d\n", best_guard
, best_minute
,
100 best_guard
* best_minute
);