Index: aoc2024.c ================================================================== --- aoc2024.c +++ aoc2024.c @@ -75,10 +75,11 @@ 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 +TODO remove linearize2d ===================================================================== */ static bool validpos(unsigned s, int r, int c) { if (r < 0) return false; if (c < 0) return false; @@ -232,10 +233,11 @@ printf("The sum of middle update numbers is {%u}.\n", accumsum); printf("The sum of middle update numbers for newly ordered updates is {%u}.\n", accumsum2); } /* === aoc202404 ======================================================= +TODO remove linearize2d ===================================================================== */ static bool masat(char *data, unsigned size, unsigned row, unsigned col, int drow, int dcol) { int maxrow = (int)row + 3*drow; int maxcol = (int)col + 3*dcol; Index: aocutils.c ================================================================== --- aocutils.c +++ aocutils.c @@ -1,9 +1,21 @@ +#include #include #include #include "aocutils.h" +bool TGvalid(struct TextGrid *tg, unsigned row, unsigned col) { + if (row >= tg->rows) return false; + if (col >= tg->cols) return false; + return true; +} +char *TGcharptr(struct TextGrid *tg, unsigned row, unsigned col) { + if (!TGvalid(tg, row, col)) return NULL; + return tg->data + (row * tg->cols) + col; +} + +// TODO: rewrite this shit size_t linearize2d(unsigned width, unsigned row, unsigned col) { return (row * width) + col; } size_t text2array(unsigned **dst, const char *r) { Index: aocutils.h ================================================================== --- aocutils.h +++ aocutils.h @@ -1,9 +1,17 @@ #ifndef AOCUTILS_H_INCLUDED #define AOCUTILS_H_INCLUDED -size_t linearize2d(unsigned width, unsigned row, unsigned col); +struct TextGrid { + unsigned rows, cols; + char *data; // may have '\n' at end of rows +}; + +bool TGvalid(struct TextGrid *tg, unsigned row, unsigned col); +char *TGcharptr(struct TextGrid *tg, unsigned row, unsigned col); + +size_t linearize2d(unsigned width, unsigned row, unsigned col); // TODO size_t text2array(unsigned **dst, const char *txt); size_t slurp(char **dst, const char *filename); unsigned distance(unsigned a, unsigned b); unsigned max3u(unsigned a, unsigned b, unsigned c); unsigned min3u(unsigned a, unsigned b, unsigned c);