From b22062d3f6e94a105434b1548ffec9da67ee8a54 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Sat, 5 Dec 2026 14:16:26 +0000 Subject: 3++ --- 2026/3.c | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to '2026') diff --git a/2026/3.c b/2026/3.c index 749dbac..232b582 100644 --- a/2026/3.c +++ b/2026/3.c @@ -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); -- cgit v1.3.1