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); ADDED test.txt Index: test.txt ================================================================== --- /dev/null +++ test.txt @@ -0,0 +1,12 @@ +............ +........0... +.....0...... +.......0.... +....0....... +......A..... +............ +............ +........A... +.........A.. +............ +............ ADDED testtg.c Index: testtg.c ================================================================== --- /dev/null +++ testtg.c @@ -0,0 +1,26 @@ +#include +#include +#include + +#include "aocutils.h" + +int main(void) { + char *input = NULL; + size_t ilen = slurp(&input, "test.txt"); + + struct TextGrid tg; + tg.data = input; + tg.cols = strchr(input, '\n') - input + 1; + tg.rows = tg.cols - 1; // rows includes the '\n' + + if (*TGcharptr(&tg, 11, 11) == '.') puts("1"); + if (TGcharptr(&tg, 12, 11) == NULL) puts("2"); + if (TGcharptr(&tg, 11, 12) == NULL) puts("3"); + if (*TGcharptr(&tg, 11, 12) == '\n') puts("33"); + if (TGcharptr(&tg, -1, -2) == NULL) puts("4"); + + for (int k = 0; k < 12; k++) { + *TGcharptr(&tg, k, k) = '#'; + } + printf("\n\n%s\n", tg.data); +}