8 void debug(const char *fmt
, ...) {
10 if (getenv("DEBUG")) {
12 vfprintf(stderr
, fmt
, ap
);
22 int main(int argc
, char **argv
) {
23 size_t len
= 0, count
= 0;
29 /* Part 1 - read in lines */
31 if (!(stdin
= freopen(argv
[1], "r", stdin
))) {
36 while (getline(&line
, &len
, stdin
) >= 0) {
38 if (sscanf(line
, "Step %c must be finished before step %c can begin. ", &a
,
39 &b
) != 2 || (unsigned)(a
-'A') > 25 || (unsigned)(b
-'A') > 25) {
40 fprintf(stderr
, "bad input\n");
43 list
[a
- 'A'].state
= 1;
44 list
[b
- 'A'].state
= 1;
45 strchrnul(list
[b
- 'A'].wait
, '\0')[0] = a
;
47 printf("Read %zu lines\n", count
);
49 /* Part 2 - determine output */
50 for (i
= 0; i
< 26; i
++)
51 for (j
= 0; j
< 26; j
++) {
52 if (list
[j
].state
!= 1)
56 if (list
[*p
- 'A'].state
< 2)
63 printf("%c", j
+ 'A');