Index: aoc2024.c ================================================================== --- aoc2024.c +++ aoc2024.c @@ -36,10 +36,24 @@ 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++; 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; + return tmp == 15; +} void aoc202404(char *data, size_t len) { (void)len; // unused argument // assume data is well-formatted and has the same number of rows and columns unsigned size = strchr(data, '\n') - data; @@ -53,17 +67,21 @@ 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)]); #endif - unsigned xmascount = 0; + 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); + if ((1 <= row) && (row < size - 1) && (1 <= col) && (col < size - 1)) { + Xmascount += Xmasat(data, size, row, col); + } } } 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); } void aoc202403(char *data, size_t len) { (void)len; // unused argument unsigned sumproducts = 0, sumproducts2 = 0, term[2];