9 static int do_debug
= -1;
10 void debug(const char *fmt
, ...) {
13 do_debug
= !!getenv("DEBUG");
16 vfprintf(stderr
, fmt
, ap
);
29 struct orbit list
[LIMIT
] = { { "", "COM", -1 }, };
36 for (int i
= 0; i
< count
; i
++)
37 debug("%s)%s %d\n", list
[i
].parent
, list
[i
].name
, list
[i
].idx
);
40 int main(int argc
, char **argv
) {
41 int i
, j
, count
= 1, total
= 0, trans
= 0;
42 int you
= -1, san
= -1, pivot
= -1;
45 if (!(stdin
= freopen(argv
[1], "r", stdin
))) {
50 /* Pass 1 - read relations: O(n) */
51 while (scanf("%3[^)])%3s ", list
[count
].parent
, list
[count
].name
) == 2) {
52 list
[count
++].idx
= -1;
54 printf("recompile with larger LIMIT\n");
58 printf("scanned %d direct relations\n", count
);
61 /* Pass 2 - locate parents: O(n^2) */
62 for (i
= 1; i
< count
; i
++) {
63 for (j
= 0; j
< count
; j
++) {
64 if (!strcmp(list
[i
].parent
, list
[j
].name
)) {
69 if (list
[i
].idx
== -1) {
70 printf("incomplete map, missing parent for %s\n", list
[i
].name
);
73 if (!strcmp(list
[i
].name
, "YOU"))
75 else if (!strcmp(list
[i
].name
, "SAN"))
80 /* Pass 3 - compute total: O(n+m) */
81 for (i
= 1; i
< count
; i
++) {
83 while (list
[j
].idx
>= 0) {
89 printf("loop detected at %d %s\n", i
, list
[i
].name
);
94 printf("%d total orbits\n", total
);
96 /* Pass 4 - count transfer orbits: O(m) */
97 if (you
== -1 || san
== -1)
98 printf("transfer computation not possible\n");
100 for (j
= san
; !list
[j
].key
; j
= list
[j
].idx
)
103 for (j
= you
; j
!= pivot
; j
= list
[j
].idx
)
105 printf("%d total transfers\n", trans
- 2);