summaryrefslogtreecommitdiff
path: root/2026/3.c
blob: 749dbac64c8619a170a2e3e5ea945e1c47257167 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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);
}