aboutsummaryrefslogtreecommitdiff
path: root/2023/16
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2023-12-19 15:54:12 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2023-12-19 15:54:12 +0100
commit1f75d7c9ebec207c8193754792076dc66eba9713 (patch)
tree48556a419f050153513cd5a3b8a4a6e125ba2c45 /2023/16
parent83adb5e4215a25d17cfc11b4dbf10cf20c418c20 (diff)
downloadaoc-1f75d7c9ebec207c8193754792076dc66eba9713.tar.gz
aoc-1f75d7c9ebec207c8193754792076dc66eba9713.zip
Added solutions for 15, 16, 17, 18 and 19
Diffstat (limited to '2023/16')
-rw-r--r--2023/16/16a.c37
-rw-r--r--2023/16/16b.c51
2 files changed, 88 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}
diff --git a/2023/16/16b.c b/2023/16/16b.c
new file mode 100644
index 0000000..ce42c31
--- /dev/null
+++ b/2023/16/16b.c
@@ -0,0 +1,51 @@
1#include <stdbool.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5
6#define M 200
7#define MAX(x,y) ((x)>(y)?(x):(y))
8
9char map[M][M];
10int s, t, n, nb, b[M][2], entered[M][M];
11
12#define E 1
13#define N 2
14#define W 4
15#define S 8
16int turn[9][255] = {
17 [E] = { ['.'] = E, ['-'] = E, ['/'] = N, ['\\'] = S, ['|'] = N|S },
18 [N] = { ['.'] = N, ['-'] = E|W, ['/'] = E, ['\\'] = W, ['|'] = N },
19 [W] = { ['.'] = W, ['-'] = W, ['/'] = S, ['\\'] = N, ['|'] = N|S },
20 [S] = { ['.'] = S, ['-'] = E|W, ['/'] = W, ['\\'] = E, ['|'] = S },
21};
22int go[9][2] = { [E] = {0, 1}, [N] = {-1, 0}, [W] = {0, -1}, [S] = {1, 0} };
23
24void walk(int i, int j, int d) {
25 if (i < 0 || j < 0 || i >= n || j >= n || (entered[i][j] & d)) return;
26 if (!entered[i][j]) s++;
27 entered[i][j] |= d;
28 for (int k = 1; k <= 8; k <<= 1)
29 if (k & turn[d][map[i][j]])
30 walk(i+go[k][0], j+go[k][1], k);
31}
32
33void clear(void) {
34 s = 0;
35 for (int i = 0; i < n; i++)
36 memset(entered[i], 0, n * sizeof(int));
37}
38
39int main() {
40 for (n = 0; fgets(map[n], M, stdin) != NULL; n++) ;
41
42 for (int i = 0; i < n; i++) {
43 clear(); walk(i, 0, E); t = MAX(s, t);
44 clear(); walk(i, n-1, W); t = MAX(s, t);
45 clear(); walk(0, i, S); t = MAX(s, t);
46 clear(); walk(n-1, i, N); t = MAX(s, t);
47 }
48
49 printf("%d\n", t);
50 return 0;
51}

Generated with cgit - Back to sebastiano.tronto.net