From 638dacd5cd74637fb25a8dfdd516dc3b4d7a92f2 Mon Sep 17 00:00:00 2001 From: Adam Spragg Date: Mon, 12 Dec 2022 10:39:38 +0000 Subject: Solve puzzle 12 part 2 --- 12.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/12.c b/12.c index 417ebdb..10dc461 100644 --- a/12.c +++ b/12.c @@ -2,6 +2,7 @@ #include #include #include +#include struct pos { @@ -86,14 +87,26 @@ tryvisit(unsigned char * range, int cols, int rows, struct pos const * from, int int -main() +main(int argc, char ** argv) { + int part = 1; unsigned char * range, * p; size_t rangesize, rangelen, bytes; int cols, rows; struct pos * front; int steps; + while ((steps = getopt(argc, argv, "p:")) != -1) { + switch (steps) { + case 'p': + part = atoi(optarg); + break; + + default: + return -1; + } + } + // Read hill range rangesize = BUFSIZ; rangelen = 0; @@ -141,9 +154,12 @@ main() struct pos * loc; for (loc = front; loc != NULL; loc = loc->next) { - if ((range[loc->x + loc->y * cols] & 0x7F) == 'S') + if (part == 1 && (range[loc->x + loc->y * cols] & 0x7F) == 'S') // We found the start point! break; + if (part == 2 && height(range[loc->x + loc->y * cols]) == 'a') + // We found a low point + break; if (tryvisit(range, cols, rows, loc, -1, 0)) nextfront = pos_create(loc->x - 1, loc->y, nextfront); -- cgit v1.2.1