diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2023-12-23 11:27:55 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2023-12-23 11:27:55 +0100 |
| commit | 28e7f356b7d7c30326b66674c06af339b350c05c (patch) | |
| tree | c265066de289808ad3e493e0a13aeb5ae57a2640 /2023/23 | |
| parent | 709005f9a565405d9413eebf8c4ccaff53f0654a (diff) | |
| download | aoc-28e7f356b7d7c30326b66674c06af339b350c05c.tar.gz aoc-28e7f356b7d7c30326b66674c06af339b350c05c.zip | |
Added solution for 23
Diffstat (limited to '2023/23')
| -rw-r--r-- | 2023/23/23a.c | 38 | ||||
| -rw-r--r-- | 2023/23/23b.c | 97 |
2 files changed, 135 insertions, 0 deletions
diff --git a/2023/23/23a.c b/2023/23/23a.c new file mode 100644 index 0000000..fb6ac7d --- /dev/null +++ b/2023/23/23a.c | |||
| @@ -0,0 +1,38 @@ | |||
| 1 | #include <stdbool.h> | ||
| 2 | #include <stdio.h> | ||
| 3 | #include <stdlib.h> | ||
| 4 | #include <string.h> | ||
| 5 | |||
| 6 | #define N 200 | ||
| 7 | |||
| 8 | bool v[N][N]; | ||
| 9 | char map[N][N]; | ||
| 10 | int n, s, t, sol; | ||
| 11 | |||
| 12 | int max(int x, int y) { return x > y ? x : y; } | ||
| 13 | |||
| 14 | int longpath(int i, int j) { | ||
| 15 | if (i == n-1 && j == t) return 0; | ||
| 16 | if (v[i][j]) return -1; | ||
| 17 | |||
| 18 | char c = map[i][j]; | ||
| 19 | int ret = -1; | ||
| 20 | v[i][j] = true; | ||
| 21 | if (c == '.' || c == '>') ret = max(ret, longpath(i, j+1)); | ||
| 22 | if (c == '.' || c == 'v') ret = max(ret, longpath(i+1, j)); | ||
| 23 | if (c == '.' || c == '<') ret = max(ret, longpath(i, j-1)); | ||
| 24 | if (c == '.' || c == '^') ret = max(ret, longpath(i-1, j)); | ||
| 25 | v[i][j] = false; | ||
| 26 | |||
| 27 | return ret == -1 ? ret : ret + 1; | ||
| 28 | } | ||
| 29 | |||
| 30 | int main() { | ||
| 31 | for (n = 1; fgets(map[n], N, stdin) != NULL; n++) ; | ||
| 32 | memset(map[0], '#', n); | ||
| 33 | for (int i = 0; i < n; i++) if (map[1][i] == '.') s = i; | ||
| 34 | for (int i = 0; i < n; i++) if (map[n-1][i] == '.') t = i; | ||
| 35 | |||
| 36 | printf("%d\n", longpath(1, s)); | ||
| 37 | return 0; | ||
| 38 | } | ||
diff --git a/2023/23/23b.c b/2023/23/23b.c new file mode 100644 index 0000000..9635805 --- /dev/null +++ b/2023/23/23b.c | |||
| @@ -0,0 +1,97 @@ | |||
| 1 | #include <stdbool.h> | ||
| 2 | #include <stdio.h> | ||
| 3 | #include <stdlib.h> | ||
| 4 | #include <string.h> | ||
| 5 | |||
| 6 | #define N 150 | ||
| 7 | #define V 1000 | ||
| 8 | |||
| 9 | typedef struct { int i, j; } node_t; | ||
| 10 | typedef struct { int u, w; } edge_t; | ||
| 11 | int d[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; | ||
| 12 | |||
| 13 | bool v[N][N]; | ||
| 14 | char map[N][N]; | ||
| 15 | int n, s, t, nv, ne[V]; | ||
| 16 | node_t node[V]; | ||
| 17 | edge_t g[V][4]; | ||
| 18 | |||
| 19 | int max(int x, int y) { return x > y ? x : y; } | ||
| 20 | |||
| 21 | bool isjunc(int i, int j) { | ||
| 22 | int nn = 0; | ||
| 23 | for (int k = 0; k < 4; k++) | ||
| 24 | nn += map[i+d[k][0]][j+d[k][1]] == '#'; | ||
| 25 | return nn != 2; | ||
| 26 | } | ||
| 27 | |||
| 28 | void advance(int io, int jo, int *i, int *j) { | ||
| 29 | for (int k = 0; k < 4; k++) { | ||
| 30 | int ni = *i + d[k][0], nj = *j + d[k][1]; | ||
| 31 | if (!v[ni][nj] && (ni != io || nj != jo) && | ||
| 32 | map[ni][nj] != '#') { | ||
| 33 | *i = ni; | ||
| 34 | *j = nj; | ||
| 35 | if (!isjunc(*i, *j)) v[*i][*j] = true; | ||
| 36 | return; | ||
| 37 | } | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | int findorcreate(int i, int j) { | ||
| 42 | for (int u = 0; u < nv; u++) | ||
| 43 | if (node[u].i == i && node[u].j == j) | ||
| 44 | return u; | ||
| 45 | node[nv] = (node_t) {.i = i, .j = j}; | ||
| 46 | return nv++; | ||
| 47 | } | ||
| 48 | |||
| 49 | edge_t findedge(int io, int jo, int i, int j) { | ||
| 50 | int w; | ||
| 51 | v[i][j] = true; | ||
| 52 | for (w = 1; !isjunc(i, j); w++) advance(io, jo, &i, &j); | ||
| 53 | return (edge_t) {.u = findorcreate(i, j), .w = w}; | ||
| 54 | } | ||
| 55 | |||
| 56 | void makegraph(int u) { | ||
| 57 | int i = node[u].i, j = node[u].j; | ||
| 58 | for (int k = 0; k < 4; k++) { | ||
| 59 | int ni = i + d[k][0], nj = j + d[k][1]; | ||
| 60 | if (!v[ni][nj] && map[ni][nj] != '#') { | ||
| 61 | edge_t e = findedge(i, j, ni, nj); | ||
| 62 | g[u][ne[u]++] = (edge_t) {.u = e.u, .w = e.w}; | ||
| 63 | g[e.u][ne[e.u]++] = (edge_t) {.u = u, .w = e.w}; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | int longpath(int u) { | ||
| 69 | int i = node[u].i, j = node[u].j, k, ret, p; | ||
| 70 | |||
| 71 | if (i == n-1 && j == t) return 0; | ||
| 72 | if (v[i][j]) return -1; | ||
| 73 | |||
| 74 | v[i][j] = true; | ||
| 75 | for (ret = -1, k = 0; k < ne[u]; k++) | ||
| 76 | if ((p = longpath(g[u][k].u)) != -1) | ||
| 77 | ret = max(ret, g[u][k].w + p); | ||
| 78 | v[i][j] = false; | ||
| 79 | |||
| 80 | return ret; | ||
| 81 | } | ||
| 82 | |||
| 83 | int main() { | ||
| 84 | for (n = 1; fgets(map[n], N, stdin) != NULL; n++) ; | ||
| 85 | memset(map[0], '#', n); | ||
| 86 | memset(map[n], '#', n); | ||
| 87 | for (int i = 0; i < n; i++) if (map[1][i] == '.') s = i; | ||
| 88 | for (int i = 0; i < n; i++) if (map[n-1][i] == '.') t = i; | ||
| 89 | |||
| 90 | findorcreate(1, s); | ||
| 91 | for (int u = 0; u < nv; u++) | ||
| 92 | makegraph(u); | ||
| 93 | |||
| 94 | printf("%d\n", longpath(0)); | ||
| 95 | |||
| 96 | return 0; | ||
| 97 | } | ||
