aboutsummaryrefslogtreecommitdiff
path: root/2023/13/13b.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/13b.c
parent2d2b30491c4987e84b4b828a7705993aaa1bb5bb (diff)
downloadaoc-2bf891c41c4613c83d869796cb68793ddb629ab8.tar.gz
aoc-2bf891c41c4613c83d869796cb68793ddb629ab8.zip
Added solution for 13
Diffstat (limited to '2023/13/13b.c')
-rw-r--r--2023/13/13b.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/2023/13/13b.c b/2023/13/13b.c
new file mode 100644
index 0000000..090a895
--- /dev/null
+++ b/2023/13/13b.c
@@ -0,0 +1,52 @@
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 onebit(int64_t x) {
14 while (x % 2 == 0) x >>= 1;
15 return !(x-1);
16}
17
18int64_t issmudgedmirror(int64_t i, int64_t a[], int64_t n) {
19 int64_t smudge = 0;
20 for (int64_t j = MAX(0, 2*i-n+2); j <= i && 2*i-j+1 < n; j++) {
21 if (a[j] == a[2*i-j+1]) continue;
22 if (onebit(a[j] ^ a[2*i-j+1]))
23 smudge++;
24 else
25 return 0;
26 }
27 return smudge == 1;
28}
29
30int main() {
31 do {
32 buf = fgets(line, N, stdin);
33 if (buf == NULL || line[0] == '\n') {
34 for (i = 0; i < nc-1; i++)
35 s += (i + 1) * issmudgedmirror(i, c, nc);
36 for (i = 0; i < nr-1; i++)
37 s += 100 * (i + 1) * issmudgedmirror(i, r, nr);
38 nr = nc = 0;
39 memset(r, 0, N * sizeof(int64_t));
40 memset(c, 0, N * sizeof(int64_t));
41 } else {
42 for (nc = 0; line[nc] != '\n'; nc++) {
43 r[nr] = (r[nr] << 1) + (line[nc] == '#');
44 c[nc] = (c[nc] << 1) + (line[nc] == '#');
45 }
46 nr++;
47 }
48 } while (buf != NULL);
49
50 printf("%" PRId64 "\n", s);
51 return 0;
52}

Generated with cgit - Back to sebastiano.tronto.net