aboutsummaryrefslogtreecommitdiff
path: root/2023/16/16a.c
diff options
context:
space:
mode:
Diffstat (limited to '2023/16/16a.c')
-rw-r--r--2023/16/16a.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/2023/16/16a.c b/2023/16/16a.c
new file mode 100644
index 0000000..0f95553
--- /dev/null
+++ b/2023/16/16a.c
@@ -0,0 +1,37 @@
1#include <stdbool.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5
6#define M 200
7
8char map[M][M];
9int s, n, nb, b[M][2], entered[M][M];
10
11#define E 1
12#define N 2
13#define W 4
14#define S 8
15int turn[9][255] = {
16 [E] = { ['.'] = E, ['-'] = E, ['/'] = N, ['\\'] = S, ['|'] = N|S },
17 [N] = { ['.'] = N, ['-'] = E|W, ['/'] = E, ['\\'] = W, ['|'] = N },
18 [W] = { ['.'] = W, ['-'] = W, ['/'] = S, ['\\'] = N, ['|'] = N|S },
19 [S] = { ['.'] = S, ['-'] = E|W, ['/'] = W, ['\\'] = E, ['|'] = S },
20};
21int go[9][2] = { [E] = {0, 1}, [N] = {-1, 0}, [W] = {0, -1}, [S] = {1, 0} };
22
23void walk(int i, int j, int d) {
24 if (i < 0 || j < 0 || i >= n || j >= n || (entered[i][j] & d)) return;
25 if (!entered[i][j]) s++;
26 entered[i][j] |= d;
27 for (int k = 1; k <= 8; k <<= 1)
28 if (k & turn[d][map[i][j]])
29 walk(i+go[k][0], j+go[k][1], k);
30}
31
32int main() {
33 for (n = 0; fgets(map[n], M, stdin) != NULL; n++) ;
34 walk(0, 0, E);
35 printf("%d\n", s);
36 return 0;
37}

Generated with cgit - Back to sebastiano.tronto.net