Index: aoc2024.c ================================================================== --- aoc2024.c +++ aoc2024.c @@ -4,10 +4,54 @@ #include #include #include #include "aocdailies.h" #include "aocutils.h" + +/* === aoc202408 ======================================================= + Oh! another square of text! + Idea: for all points p with an antenna find all points q>p with +a corresponding frequency. For each such pair calculate the 2 antinodes +and add the resulting points (if not there already) to an array. + The answer to Part One is the number of elements in the array + TODO: I don't like my `linearize2d` thing! Thinking of a replacement +===================================================================== */ + +void aoc202408(char *data, size_t len) { + (void)len; // unused argument + unsigned anti[256][2]; // locations of antinodes + unsigned nanti = 0; + unsigned size = 1 + strchr(data, '\n') - data; + for (unsigned row = 0; row < size-1; row++) { + for (unsigned col = 0; col < size-1; col++) { + char *p = data + linearize2d(size, row, col); + if (*p != '.') { + char *q = strchr(p + 1, *p); + while (q) { + printf("found '%c'('%c')\n", *p, *q); + ptrdiff_t delta = q - p; + #if 0 + int row_pos = (int)(linear_pos / size); + int col_pos = (int)(linear_pos % size); + #endif + if (p >= data + delta) { + // antinode + anti[nanti][0] = row(p - delta); + anti[nanti++][1] = col(p - delta); + } + if (q < "end of data" - delta) { + // antinode + anti[nanti][0] = row(q + delta); + anti[nanti++][1] = col(q + delta); + } + q = strchr(q + 1, *p); + } + } + } + } + printf("There are %u antinodes within the map.\n", nanti); +} /* === aoc202407 ======================================================= Part one looks easy Part two also easy ===================================================================== */ @@ -75,10 +119,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 +277,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: aocdailies.c ================================================================== --- aocdailies.c +++ aocdailies.c @@ -5,10 +5,11 @@ aocfunc *p; switch (y * 100 + d) { default: p = NULL; break; // YYYYdd ==> aocYYYYdd + case 202408: p = aoc202408; break; case 202407: p = aoc202407; break; case 202406: p = aoc202406; break; case 202405: p = aoc202405; break; case 202404: p = aoc202404; break; case 202403: p = aoc202403; break; Index: aocdailies.h ================================================================== --- aocdailies.h +++ aocdailies.h @@ -4,10 +4,11 @@ #include typedef void aocfunc(char *, size_t); aocfunc *aocselect(unsigned, unsigned); +aocfunc aoc202408; aocfunc aoc202407; aocfunc aoc202406; aocfunc aoc202405; aocfunc aoc202404; aocfunc aoc202403; Index: aocutils.c ================================================================== --- aocutils.c +++ aocutils.c @@ -1,9 +1,27 @@ +#include #include #include #include "aocutils.h" +struct TextGrid { + unsigned rows, cols; + char *data; // may have '\n' at end of rows +}; + +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 (row >= tg->rows) return NULL; + if (col >= tg->cols) return NULL; + return 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);