aboutsummaryrefslogtreecommitdiff
path: root/2023/13/13a.c
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2023-12-13 23:01:17 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2023-12-13 23:01:17 +0100
commit2bf891c41c4613c83d869796cb68793ddb629ab8 (patch)
tree2de647ec14600476c1fe994c47fab95d0055d5aa /2023/13/13a.c
parent2d2b30491c4987e84b4b828a7705993aaa1bb5bb (diff)
downloadaoc-2bf891c41c4613c83d869796cb68793ddb629ab8.tar.gz
aoc-2bf891c41c4613c83d869796cb68793ddb629ab8.zip
Added solution for 13
Diffstat (limited to '2023/13/13a.c')
-rw-r--r--2023/13/13a.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/2023/13/13a.c b/2023/13/13a.c
new file mode 100644
index 0000000..c7c0b3a
--- /dev/null
+++ b/2023/13/13a.c
@@ -0,0 +1,42 @@
1#include <inttypes.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5
6#define N 64
7
8#define MAX(a,b) ((a)>(b)?(a):(b))
9
10char *buf, line[N];
11int64_t i, s, nc, nr, r[N], c[N];
12
13int64_t ismirror(int64_t i, int64_t a[], int64_t n) {
14 for (int64_t j = MAX(0, 2*i-n+2); j <= i && 2*i-j+1 < n; j++)
15 if (a[j] != a[2*i-j+1])
16 return 0;
17 return 1;
18}
19
20int main() {
21 do {
22 buf = fgets(line, N, stdin);
23 if (buf == NULL || line[0] == '\n') {
24 for (i = 0; i < nc-1; i++)
25 s += (i + 1) * ismirror(i, c, nc);
26 for (i = 0; i < nr-1; i++)
27 s += 100 * (i + 1) * ismirror(i, r, nr);
28 nr = nc = 0;
29 memset(r, 0, N * sizeof(int64_t));
30 memset(c, 0, N * sizeof(int64_t));
31 } else {
32 for (nc = 0; line[nc] != '\n'; nc++) {
33 r[nr] = (r[nr] << 1) + (line[nc] == '#');
34 c[nc] = (c[nc] << 1) + (line[nc] == '#');
35 }
36 nr++;
37 }
38 } while (buf != NULL);
39
40 printf("%" PRId64 "\n", s);
41 return 0;
42}

Generated with cgit - Back to sebastiano.tronto.net