aoc

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

16b.c (1250B)


      1 #include <stdio.h>
      2 #include <string.h>
      3 
      4 #define M 200
      5 #define MAX(x,y) ((x)>(y)?(x):(y))
      6 
      7 char map[M][M];
      8 int s, t, n, nb, b[M][2], entered[M][M];
      9 
     10 #define E 1
     11 #define N 2
     12 #define W 4
     13 #define S 8
     14 int turn[9][255] = {
     15 	[E] = { ['.'] = E, ['-'] = E,   ['/'] = N, ['\\'] = S, ['|'] = N|S },
     16 	[N] = { ['.'] = N, ['-'] = E|W, ['/'] = E, ['\\'] = W, ['|'] = N   },
     17 	[W] = { ['.'] = W, ['-'] = W,   ['/'] = S, ['\\'] = N, ['|'] = N|S },
     18 	[S] = { ['.'] = S, ['-'] = E|W, ['/'] = W, ['\\'] = E, ['|'] = S   },
     19 };
     20 int go[9][2] = { [E] = {0, 1}, [N] = {-1, 0}, [W] = {0, -1}, [S] = {1, 0} };
     21 
     22 void walk(int i, int j, int d) {
     23 	if (i < 0 || j < 0 || i >= n || j >= n || (entered[i][j] & d)) return;
     24 	if (!entered[i][j]) s++;
     25 	entered[i][j] |= d;
     26 	for (int k = 1; k <= 8; k <<= 1)
     27 		if (k & turn[d][(int)map[i][j]])
     28 			walk(i+go[k][0], j+go[k][1], k);
     29 }
     30 
     31 void clear(void) {
     32 	s = 0;
     33 	for (int i = 0; i < n; i++)
     34 		memset(entered[i], 0, n * sizeof(int));
     35 }
     36 
     37 int main() {
     38 	for (n = 0; fgets(map[n], M, stdin) != NULL; n++) ;
     39 
     40 	for (int i = 0; i < n; i++) {
     41 		clear(); walk(i, 0,   E); t = MAX(s, t);
     42 		clear(); walk(i, n-1, W); t = MAX(s, t);
     43 		clear(); walk(0,   i, S); t = MAX(s, t);
     44 		clear(); walk(n-1, i, N); t = MAX(s, t);
     45 	}
     46 
     47 	printf("%d\n", t);
     48 	return 0;
     49 }