Index: aoc2024.c ================================================================== --- aoc2024.c +++ aoc2024.c @@ -5,14 +5,22 @@ #include #include #include "aocdailies.h" #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` +===================================================================== */ + void aoc202406(char *data, size_t len) { (void)data; (void)len; } + +/* === aoc202405 ======================================================= +===================================================================== */ static int ppcmp(unsigned a, unsigned b, unsigned (*r)[2], size_t nr) { for (size_t k = 0; k < nr; k++) { if ((a == r[k][0]) && (b == r[k][1])) return -1; if ((a == r[k][1]) && (b == r[k][0])) return 1; @@ -81,30 +89,29 @@ } while ((line = strtok(NULL, "\n")) != NULL); 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); } -static size_t rcmap(unsigned size, unsigned row, unsigned col) { - return row * (size + 1) + col; -} +/* === aoc202404 ======================================================= +===================================================================== */ 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; if ((0 <= maxrow) && (maxrow < (int)size) && (0 <= maxcol) && (maxcol < (int)size)) { - if (data[rcmap(size, (unsigned)((int)row + drow), (unsigned)((int)col + dcol))] != 'M') return false; - if (data[rcmap(size, (unsigned)((int)row + 2*drow), (unsigned)((int)col + 2*dcol))] != 'A') return false; - if (data[rcmap(size, (unsigned)((int)row + 3*drow), (unsigned)((int)col + 3*dcol))] != 'S') return false; + if (data[linearize2d(size+1, (unsigned)((int)row + drow), (unsigned)((int)col + dcol))] != 'M') return false; + if (data[linearize2d(size+1, (unsigned)((int)row + 2*drow), (unsigned)((int)col + 2*dcol))] != 'A') return false; + if (data[linearize2d(size+1, (unsigned)((int)row + 3*drow), (unsigned)((int)col + 3*dcol))] != 'S') return false; } else { return false; } return true; } static unsigned xmasat(char *data, unsigned size, unsigned row, unsigned col) { unsigned finds = 0; - if (data[rcmap(size, row, col)] != 'X') return 0; + if (data[linearize2d(size+1, row, col)] != 'X') return 0; if (masat(data, size, row, col, -1, -1)) finds++; if (masat(data, size, row, col, -1, 0)) finds++; if (masat(data, size, row, col, -1, +1)) finds++; if (masat(data, size, row, col, 0, -1)) finds++; if (masat(data, size, row, col, 0, +1)) finds++; @@ -114,19 +121,19 @@ return finds; } static bool Xmasat(char *data, unsigned size, unsigned row, unsigned col) { unsigned tmp = 0; - if (data[rcmap(size, row, col)] != 'A') return false; - if (data[rcmap(size, row-1, col-1)] == 'M') tmp |= 1; - if (data[rcmap(size, row-1, col-1)] == 'S') tmp |= 2; - if (data[rcmap(size, row-1, col+1)] == 'M') tmp |= 4; - if (data[rcmap(size, row-1, col+1)] == 'S') tmp |= 8; - if (data[rcmap(size, row+1, col+1)] == 'M') tmp |= 1; - if (data[rcmap(size, row+1, col+1)] == 'S') tmp |= 2; - if (data[rcmap(size, row+1, col-1)] == 'M') tmp |= 4; - if (data[rcmap(size, row+1, col-1)] == 'S') tmp |= 8; + if (data[linearize2d(size+1, row, col)] != 'A') return false; + if (data[linearize2d(size+1, row-1, col-1)] == 'M') tmp |= 1; + if (data[linearize2d(size+1, row-1, col-1)] == 'S') tmp |= 2; + if (data[linearize2d(size+1, row-1, col+1)] == 'M') tmp |= 4; + if (data[linearize2d(size+1, row-1, col+1)] == 'S') tmp |= 8; + if (data[linearize2d(size+1, row+1, col+1)] == 'M') tmp |= 1; + if (data[linearize2d(size+1, row+1, col+1)] == 'S') tmp |= 2; + if (data[linearize2d(size+1, row+1, col-1)] == 'M') tmp |= 4; + if (data[linearize2d(size+1, row+1, col-1)] == 'S') tmp |= 8; return (tmp == 15); } void aoc202404(char *data, size_t len) { (void)len; // unused argument @@ -137,14 +144,14 @@ size, data[0], data[1], data[1*(size+1)], data[1*(size+1)+1], data[2*(size+1)], data[2*(size+1)+1]); printf("ends with %c%c.\n", data[(size-1)*(size+1) + size-2], data[(size-1)*(size+1)+size-1]); printf("data has %u cols, first three rows start with %c%c, %c%c, and %c%c.\n", - size, data[rcmap(size, 0, 0)], data[rcmap(size, 0, 1)], - data[rcmap(size, 1, 0)], data[rcmap(size, 1, 1)], - data[rcmap(size, 2, 0)], data[rcmap(size, 2, 1)]); - printf("ends with %c%c.\n", data[rcmap(size, size-1, size-2)], data[rcmap(size, size-1, size-1)]); + size, data[linearize2d(size+1, 0, 0)], data[linearize2d(size+1, 0, 1)], + data[linearize2d(size+1, 1, 0)], data[linearize2d(size+1, 1, 1)], + data[linearize2d(size+1, 2, 0)], data[linearize2d(size+1, 2, 1)]); + printf("ends with %c%c.\n", data[linearize2d(size+1, size-1, size-2)], data[linearize2d(size+1, size-1, size-1)]); #endif unsigned xmascount = 0, Xmascount = 0; for (unsigned row = 0; row < size; row++) { for (unsigned col = 0; col < size; col++) { xmascount += xmasat(data, size, row, col); @@ -154,10 +161,13 @@ } } printf("XMAS appears %u times in the little Elf's word search.\n", xmascount); printf("X-MAS appears %u times in the little Elf's word search.\n", Xmascount); } + +/* === aoc202403 ======================================================= +===================================================================== */ void aoc202403(char *data, size_t len) { (void)len; // unused argument unsigned sumproducts = 0, sumproducts2 = 0, term[2]; char *rest = data; @@ -201,10 +211,13 @@ } printf("The sum of the products is {%u}.\n", sumproducts); printf("The sum of the products with conditionals is {%u}.\n", sumproducts2); } +/* === aoc202402 ======================================================= +===================================================================== */ + static bool safereport(unsigned *v, size_t nv) { int dir = 1; // ascending if (v[0] > v[1]) dir = -1; // descending for (size_t k = 1; k < nv; k++) { if (v[k-1] == v[k]) return false; @@ -245,10 +258,13 @@ line = strtok(NULL, "\n"); } printf("There are %u safe reports.\n", safe); printf("There are %u safe reports with the Problem Dampener.\n", safe2); } + +/* === aoc202401 ======================================================= +===================================================================== */ static int delta(const void *a, const void *b) { const unsigned *aa = a; const unsigned *bb = b; if (*aa > *bb) return 1; Index: aocutils.c ================================================================== --- aocutils.c +++ aocutils.c @@ -1,8 +1,12 @@ #include #include #include "aocutils.h" + +size_t linearize2d(unsigned width, unsigned row, unsigned col) { + return (row * width) + col; +} size_t text2array(unsigned **dst, const char *r) { unsigned *a = malloc(512 * sizeof *a); size_t na = 0, sa = 512; char *err; Index: aocutils.h ================================================================== --- aocutils.h +++ aocutils.h @@ -1,8 +1,9 @@ #ifndef AOCUTILS_H_INCLUDED #define AOCUTILS_H_INCLUDED +size_t linearize2d(unsigned width, unsigned row, unsigned col); 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);