Index: aoc2024.c ================================================================== --- aoc2024.c +++ aoc2024.c @@ -8,10 +8,13 @@ #include "aocutils.h" /* === aoc202406 ======================================================= Hmmm, I've done something like this already this year... on day 4 I need rcmap() moved to aocutils ... done; now it's called `linearize2d` +Part Two: brute force? It likely is faster than coming up with an algo + Let's go: save map, for every available position put in an obstacle, + loop around until some place is visited 4? times or area is exited ===================================================================== */ static bool validpos(unsigned s, int r, int c) { if (r < 0) return false; if (c < 0) return false; @@ -24,11 +27,12 @@ if (*drow) { *dcol = -*drow; *drow = 0; } else { *drow = *dcol; *dcol = 0; } } void aoc202406(char *data, size_t len) { - (void)len; + char *savedmap = malloc(len + 1); + strcpy(savedmap, data); // assume data is well-formatted and has the same number of rows and columns unsigned size = 1 + strchr(data, '\n') - data; unsigned linear_pos = strchr(data, '^') - data; int row_pos = (int)(linear_pos / size); int col_pos = (int)(linear_pos % size); @@ -47,10 +51,12 @@ col_pos += deltacol; break; } } printf("The guard visits %u positions before leaving the area.\n", visited); + // Part Two + free(savedmap); } /* === aoc202405 ======================================================= ===================================================================== */