From cac30055faf199136f9fa14b3ed866c9b78ed022 Mon Sep 17 00:00:00 2001 From: Adam Spragg Date: Mon, 12 Dec 2022 10:29:51 +0000 Subject: Don't infinite loop if we never reach the end point --- 12.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/12.c b/12.c index c647410..42d63d4 100644 --- a/12.c +++ b/12.c @@ -136,7 +136,7 @@ main() range[front->x + front->y * cols] |= 0x80; // Take steps, widening the search front each iteration. - for (steps = 0; 1; ++steps) { + for (steps = 0; front != NULL; ++steps) { struct pos * nextfront = NULL; struct pos * loc; @@ -166,7 +166,10 @@ main() front = nextfront; } - printf("Found end at %d,%d in %d steps\n", front->x, front->y, steps); + if (front) + printf("Found end at %d,%d in %d steps\n", front->x, front->y, steps); + else + printf("No end found after %d steps\n", steps); pos_free(front); free(range); -- cgit v1.2.1