aoc

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

11a.c (709B)


      1 #include <inttypes.h>
      2 #include <stdio.h>
      3 
      4 #define N 1000
      5 
      6 #define ABS(a) ((a)>0 ? (a) : -(a))
      7 
      8 uint8_t dr[N], dc[N];
      9 int64_t i, j, k, n, p, g[N*N][2];
     10 
     11 int main() {
     12 	char line[N];
     13 	for (i = 0; fgets(line, N, stdin) != NULL; i++) {
     14 		for (j = 0; line[j] != '\n'; j++) {
     15 			if (line[j] == '#') {
     16 				g[n][0] = i;
     17 				g[n++][1] = j;
     18 				dr[i] = dc[j] = 1;
     19 			}
     20 		}
     21 	}
     22 
     23 	for (k = 1; k < i; k++) dr[k] += dr[k-1];
     24 	for (k = 1; k < j; k++) dc[k] += dc[k-1];
     25 
     26 	for (i = 0; i < n; i++) {
     27 		g[i][0] += g[i][0] - dr[g[i][0]];
     28 		g[i][1] += g[i][1] - dc[g[i][1]];
     29 	}
     30 
     31 	for (i = 0; i < n; i++)
     32 		for (j = i+1; j < n; j++)
     33 			p += ABS(g[i][0] - g[j][0]) + ABS(g[i][1] - g[j][1]);
     34 
     35 	printf("%" PRId64 "\n", p);
     36 	return 0;
     37 }