10 static int a
[LIMIT
*2][LIMIT
*2];
12 static int do_debug
= -1;
13 void debug(const char *fmt
, ...) {
16 do_debug
= !!getenv("DEBUG");
19 vfprintf(stderr
, fmt
, ap
);
24 enum dir
{ U
, R
, D
, L
};
26 int main(int argc
, char **argv
) {
27 int i
, d
, count
= 0, line
= 0, len
= 0;
28 int x
= LIMIT
, y
= LIMIT
;
29 int best1
= LIMIT
* 2, best2
= INT_MAX
;
32 if (!(stdin
= freopen(argv
[1], "r", stdin
))) {
40 case 'U': d
= U
; break;
41 case 'R': d
= R
; break;
42 case 'D': d
= D
; break;
43 case 'L': d
= L
; break;
45 printf("unexpected input at %d\n", count
);
48 if (scanf("%d", &i
) != 1) {
49 printf("unexpected input at %d\n", count
);
60 if (x
< 0 || x
>= LIMIT
*2 || y
< 0 || y
>= LIMIT
*2 || len
> INT_MAX
) {
61 printf("recompile with larger LIMIT\n");
66 debug("intersect at %d,%d time %d+%d=%d\n", x
-LIMIT
, y
-LIMIT
,
67 a
[x
][y
], len
, a
[x
][y
] + len
);
68 if (abs(x
-LIMIT
) + abs(y
-LIMIT
) < best1
)
69 best1
= abs(x
-LIMIT
) + abs(y
-LIMIT
);
70 if (a
[x
][y
] + len
< best2
)
71 best2
= a
[x
][y
] + len
;
73 } else if (!a
[x
][y
]) {
80 debug("parsed a line len=%d\n", len
);
86 printf("unexpected input at %d\n", count
);
90 printf("after %d segments, best distance %d, best time %d\n", count
,