aoc

My solutions for the Advent of Code
git clone https://git.tronto.net/aoc
Download | Log | Files | Refs | README

13a.c (893B)


      1 #include <inttypes.h>
      2 #include <stdio.h>
      3 #include <string.h>
      4 
      5 #define N 64
      6 
      7 #define MAX(a,b) ((a)>(b)?(a):(b))
      8 
      9 char *buf, line[N];
     10 int64_t i, s, nc, nr, r[N], c[N];
     11 
     12 int64_t ismirror(int64_t i, int64_t a[], int64_t n) {
     13 	for (int64_t j = MAX(0, 2*i-n+2); j <= i && 2*i-j+1 < n; j++)
     14 		if (a[j] != a[2*i-j+1])
     15 			return 0;
     16 	return 1;
     17 }
     18 
     19 int main() {
     20 	do {
     21 		buf = fgets(line, N, stdin);
     22 		if (buf == NULL || line[0] == '\n') {
     23 			for (i = 0; i < nc-1; i++)
     24 				s += (i + 1) * ismirror(i, c, nc);
     25 			for (i = 0; i < nr-1; i++)
     26 				s += 100 * (i + 1) * ismirror(i, r, nr);
     27 			nr = nc = 0;
     28 			memset(r, 0, N * sizeof(int64_t));
     29 			memset(c, 0, N * sizeof(int64_t));
     30 		} else {
     31 			for (nc = 0; line[nc] != '\n'; nc++) {
     32 				r[nr] = (r[nr] << 1) + (line[nc] == '#');
     33 				c[nc] = (c[nc] << 1) + (line[nc] == '#');
     34 			}
     35 			nr++;
     36 		}
     37 	} while (buf != NULL);
     38 
     39 	printf("%" PRId64 "\n", s);
     40 	return 0;
     41 }