10 bool debug(const char *fmt
, ...) {
12 if (getenv("DEBUG")) {
14 vfprintf(stderr
, fmt
, ap
);
22 #define OFFSET (LIMIT / 2)
23 static short grid
[LIMIT
][LIMIT
];
24 typedef struct state
{
30 static const char *orig
;
32 static void set(int x
, int y
, int d
) {
34 debug("skipping %d,%d to %d, already visited at %d\n",
40 static void visit(state
*s
) {
41 short d
= grid
[s
->y
][s
->x
];
46 debug("depth %d, visiting %c (%tu) at %d,%d distance %d\n",
47 s
->depth
, *s
->p
, s
->p
- orig
, s
->x
, s
->y
, d
);
48 if (s
->x
< 1 || s
->x
>= LIMIT
- 1 || s
->y
< 1 || s
->y
>= LIMIT
- 1) {
49 fprintf(stderr
, "recompile with larger limit\n");
63 set(s
->x
, --s
->y
, d
+ 1);
66 set(s
->x
, ++s
->y
, d
+ 1);
69 set(++s
->x
, s
->y
, d
+ 1);
72 set(--s
->x
, s
->y
, d
+ 1);
76 if (++b
== sizeof branches
/ sizeof *branches
) {
77 fprintf(stderr
, "recompile for more alternates\n");
82 branches
[b
].p
= b
? branches
[b
- 1].p
+ 1 : s
->p
+ 1;
84 } while (*branches
[b
].p
== '|');
85 for (i
= 0; i
< b
; i
++) {
86 if (branches
[i
].x
!= branches
[b
].x
|| branches
[i
].y
!= branches
[b
].y
) {
88 branches
[i
].p
= branches
[b
].p
+ 1;
91 debug("skipping round-trip alternative\n");
104 int main(int argc
, char **argv
) {
105 size_t len
= 0, count
= 0;
108 state s
= { .x
= OFFSET
, .y
= OFFSET
};
114 if (argc
> 1 && *argv
[1] && *argv
[1] == '^')
117 if (argc
> 1 && *argv
[1] && !(stdin
= freopen(argv
[1], "r", stdin
))) {
121 if (getline(&line
, &len
, stdin
) < 0) {
122 fprintf(stderr
, "invalid input\n");
130 for (y
= 0; y
< LIMIT
; y
++)
131 for (x
= 0; x
< LIMIT
; x
++) {
132 if (grid
[y
][x
] > far
)
134 if (grid
[y
][x
] > score
)
137 printf("Furthest point is %d doors away, %zu rooms beyond %d\n",
138 score
- 1, count
, far
);