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
|
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#define N 64
#define MAX(a,b) ((a)>(b)?(a):(b))
char *buf, line[N];
int64_t i, s, nc, nr, r[N], c[N];
int64_t onebit(int64_t x) {
while (x % 2 == 0) x >>= 1;
return !(x-1);
}
int64_t issmudgedmirror(int64_t i, int64_t a[], int64_t n) {
int64_t smudge = 0;
for (int64_t j = MAX(0, 2*i-n+2); j <= i && 2*i-j+1 < n; j++) {
if (a[j] == a[2*i-j+1]) continue;
if (onebit(a[j] ^ a[2*i-j+1]))
smudge++;
else
return 0;
}
return smudge == 1;
}
int main() {
do {
buf = fgets(line, N, stdin);
if (buf == NULL || line[0] == '\n') {
for (i = 0; i < nc-1; i++)
s += (i + 1) * issmudgedmirror(i, c, nc);
for (i = 0; i < nr-1; i++)
s += 100 * (i + 1) * issmudgedmirror(i, r, nr);
nr = nc = 0;
memset(r, 0, N * sizeof(int64_t));
memset(c, 0, N * sizeof(int64_t));
} else {
for (nc = 0; line[nc] != '\n'; nc++) {
r[nr] = (r[nr] << 1) + (line[nc] == '#');
c[nc] = (c[nc] << 1) + (line[nc] == '#');
}
nr++;
}
} while (buf != NULL);
printf("%" PRId64 "\n", s);
return 0;
}
|