diff options
Diffstat (limited to '2026')
| -rw-r--r-- | 2026/3.c | 172 |
1 files changed, 172 insertions, 0 deletions
diff --git a/2026/3.c b/2026/3.c new file mode 100644 index 0000000..749dbac --- /dev/null +++ b/2026/3.c @@ -0,0 +1,172 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <string.h> +#include <assert.h> + + +#if 1 +#define DEBUG1(...) __VA_ARGS__ +#else +#define DEBUG1(...) +#endif +#define DEBUG0(...) + + +struct Input { + int w, h, stride; + char *buf; +}; + +static inline bool input_at(const struct Input *inp, int x, int y) { + return inp->buf[inp->stride * y + x] == '#'; +} +#define AT(inp_, x_, y_) input_at(&inp_, x_, y_) + +static struct Input read_input() { + const size_t size = 24*1024; + char *buf = malloc(size); + const size_t nr = fread(buf, 1, size, stdin); + assert(nr < size); // lazy + const int linelen = strchr(buf, '\n') - buf; + return (struct Input){ + .w = linelen, .h = (nr + 1) / (linelen + 1), .stride = linelen + 1, + .buf = buf, + }; +} + +int main(void) { + const struct Input in = read_input(); + + // part 1, optimised (O(w*h + N^1.5)) + + int N = 0; + for (int y = 0; y < in.h; y++) + for (int x = 0; x < in.w; x++) + N += AT(in, x, y); + + int *treex = malloc(N * sizeof(int)); + int *treey = malloc(N * sizeof(int)); + int *colcounts = calloc(in.w, sizeof(int)); + int *rowcounts = calloc(in.h, sizeof(int)); + for (int y = 0, i = 0; y < in.h; y++) + for (int x = 0; x < in.w; x++) { + if (AT(in, x, y)) { + treex[i] = x; treey[i] = y; i++; + colcounts[x]++; rowcounts[y]++; + } + } + + DEBUG0({ + fprintf(stderr, "trees"); + for (int i = 0; i < N; i++) + fprintf(stderr, " (%d,%d)", treex[i], treey[i]); + fprintf(stderr, "\n"); + }); + + // lists of "the other coordinate" for trees in each column and each row, as flattened arrays + int *collistsoff = malloc(in.w * sizeof(int)); + int *rowlistsoff = malloc(in.h * sizeof(int)); + collistsoff[0] = 0; rowlistsoff[0] = 0; + for (int x = 1; x < in.w; x++) collistsoff[x] = collistsoff[x-1] + colcounts[x-1]; + for (int y = 1; y < in.h; y++) rowlistsoff[y] = rowlistsoff[y-1] + rowcounts[y-1]; + + memset(colcounts, 0, in.w * sizeof(int)); + memset(rowcounts, 0, in.w * sizeof(int)); + int *collistsbuf = malloc(N * sizeof(int)); + int *rowlistsbuf = malloc(N * sizeof(int)); +#define COLLIST(x_, j_) collistsbuf[collistsoff[x_] + j_] +#define ROWLIST(y_, j_) rowlistsbuf[rowlistsoff[y_] + j_] + int *collistidx = malloc(N * sizeof(int)); // where in its collist/rowlist does this tree sit + int *rowlistidx = malloc(N * sizeof(int)); + for (int i = 0; i < N; i++) { + const int x = treex[i], y = treey[i]; + COLLIST(x, colcounts[x]) = y; + ROWLIST(y, rowcounts[y]) = x; + collistidx[i] = colcounts[x]++; + rowlistidx[i] = rowcounts[y]++; + } + + DEBUG0({ + fprintf(stderr, "collists:\n"); + for (int x = 0; x < in.w; x++) { + fprintf(stderr, "-"); + for (int j = 0; j < colcounts[x]; j++) + fprintf(stderr, " %d", COLLIST(x, j)); + fprintf(stderr, "\n"); + } + fprintf(stderr, "rowlists:\n"); + for (int y = 0; y < in.h; y++) { + fprintf(stderr, "-"); + for (int j = 0; j < rowcounts[y]; j++) + fprintf(stderr, " %d", ROWLIST(y, j)); + fprintf(stderr, "\n"); + } + + fprintf(stderr, "indices:\n"); + for (int i = 0; i < N; i++) { + fprintf(stderr, "- (%d,%d): %d, %d\n", treex[i], treey[i], collistidx[i], rowlistidx[i]); + } + }) + + int nsquares = 0; + + for (int i = 0; i < N; i++) { + const int x = treex[i], y = treey[i]; + const int cli = collistidx[i], rli = rowlistidx[i]; + const int collistlen = colcounts[x] - cli - 1; + const int rowlistlen = rowcounts[y] - rli - 1; + + DEBUG0({ fprintf(stderr, "TREE %d (%d,%d) cl %d(%d) rl %d(%d)\n", i, x, y, cli, collistlen, rli, rowlistlen); }); + + // loop over collist or rowlist and check for the other two corners + if (collistlen < rowlistlen) { + const int *collist = &COLLIST(x, cli) + 1; + for (int cl_idx = 0; cl_idx < collistlen; cl_idx++) { + const int y2 = collist[cl_idx]; + const int x2 = x + (y2 - y); + if (x2 < in.w && AT(in, x2, y) && AT(in, x2, y2)) nsquares++; + } + } else { + const int *rowlist = &ROWLIST(y, rli) + 1; + for (int rl_idx = 0; rl_idx < rowlistlen; rl_idx++) { + const int x2 = rowlist[rl_idx]; + const int y2 = y + (x2 - x); + if (y2 < in.h && AT(in, x, y2) && AT(in, x2, y2)) nsquares++; + } + } + } + + printf("%d\n", nsquares); + + free(colcounts); free(rowcounts); + free(collistsoff); free(rowlistsoff); + free(collistsbuf); free(rowlistsbuf); + free(collistidx); free(rowlistidx); + + // part 2, not optimised (O(w*h + N^2)) + + int nrotsquares = 0; + + // loop over left edges + for (int i1 = 0; i1 < N; i1++) { + const int x1 = treex[i1], y1 = treey[i1]; + for (int i2 = i1+1; i2 < N; i2++) { + const int x2 = treex[i2], y2 = treey[i2]; + // avoid double counting: p2 must be below or below-right of p1 + if (y2 == y1 || x2 < x1) continue; + const int dx = x2 - x1, dy = y2 - y1; + + const int x3 = x1 + dy, y3 = y1 - dx; + const int x4 = x3 + dx, y4 = y3 + dy; + if (x4 >= in.w || y3 < 0 || y4 >= in.h) continue; + if (AT(in, x3, y3) && AT(in, x4, y4)) nrotsquares++; + } + } + + printf("%d\n", nrotsquares); + + free(treex); free(treey); + + free(in.buf); +} |
