diff options
| -rw-r--r-- | 2026/3.c | 36 |
1 files changed, 35 insertions, 1 deletions
@@ -38,6 +38,7 @@ static struct Input read_input() { int main(void) { const struct Input in = read_input(); +#if 1 // part 1, optimised (O(w*h + N^1.5)) int N = 0; @@ -143,7 +144,39 @@ int main(void) { free(collistsoff); free(rowlistsoff); free(collistsbuf); free(rowlistsbuf); free(collistidx); free(rowlistidx); +#else + // part 1, not optimised (O(w*h + N^2)) + + 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)); + 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++; } + + int nsquares = 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]; + if (y2 == y1 || x2 != x1) continue; + const int d = y2 - y1; + + if (x1 + d < in.w && AT(in, x1 + d, y1) && AT(in, x1 + d, y2)) nsquares++; + } + } + + printf("%d\n", nsquares); +#endif + +#if 1 // part 2, not optimised (O(w*h + N^2)) int nrotsquares = 0; @@ -158,13 +191,14 @@ int main(void) { const int dx = x2 - x1, dy = y2 - y1; const int x3 = x1 + dy, y3 = y1 - dx; - const int x4 = x3 + dx, y4 = y3 + dy; + const int x4 = x2 + dy, y4 = y2 - dx; if (x4 >= in.w || y3 < 0 || y4 >= in.h) continue; if (AT(in, x3, y3) && AT(in, x4, y4)) nrotsquares++; } } printf("%d\n", nrotsquares); +#endif free(treex); free(treey); |
