aboutsummaryrefslogtreecommitdiff
path: root/2023/23/23a.c
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2023-12-23 11:27:55 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2023-12-23 11:27:55 +0100
commit28e7f356b7d7c30326b66674c06af339b350c05c (patch)
treec265066de289808ad3e493e0a13aeb5ae57a2640 /2023/23/23a.c
parent709005f9a565405d9413eebf8c4ccaff53f0654a (diff)
downloadaoc-28e7f356b7d7c30326b66674c06af339b350c05c.tar.gz
aoc-28e7f356b7d7c30326b66674c06af339b350c05c.zip
Added solution for 23
Diffstat (limited to '2023/23/23a.c')
-rw-r--r--2023/23/23a.c38
1 files changed, 38 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
8bool v[N][N];
9char map[N][N];
10int n, s, t, sol;
11
12int max(int x, int y) { return x > y ? x : y; }
13
14int 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
30int 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}

Generated with cgit - Back to sebastiano.tronto.net