aoc

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

16a.c (899B)


      1 #include <stdio.h>
      2 
      3 #define M 200
      4 
      5 char map[M][M];
      6 int s, n, nb, b[M][2], entered[M][M];
      7 
      8 #define E 1
      9 #define N 2
     10 #define W 4
     11 #define S 8
     12 int turn[9][255] = {
     13 	[E] = { ['.'] = E, ['-'] = E,   ['/'] = N, ['\\'] = S, ['|'] = N|S },
     14 	[N] = { ['.'] = N, ['-'] = E|W, ['/'] = E, ['\\'] = W, ['|'] = N   },
     15 	[W] = { ['.'] = W, ['-'] = W,   ['/'] = S, ['\\'] = N, ['|'] = N|S },
     16 	[S] = { ['.'] = S, ['-'] = E|W, ['/'] = W, ['\\'] = E, ['|'] = S   },
     17 };
     18 int go[9][2] = { [E] = {0, 1}, [N] = {-1, 0}, [W] = {0, -1}, [S] = {1, 0} };
     19 
     20 void walk(int i, int j, int d) {
     21 	if (i < 0 || j < 0 || i >= n || j >= n || (entered[i][j] & d)) return;
     22 	if (!entered[i][j]) s++;
     23 	entered[i][j] |= d;
     24 	for (int k = 1; k <= 8; k <<= 1)
     25 		if (k & turn[d][(int)map[i][j]])
     26 			walk(i+go[k][0], j+go[k][1], k);
     27 }
     28 
     29 int main() {
     30 	for (n = 0; fgets(map[n], M, stdin) != NULL; n++) ;
     31 	walk(0, 0, E);
     32 	printf("%d\n", s);
     33 	return 0;
     34 }