diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2023-12-22 16:44:48 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2023-12-22 16:44:48 +0100 |
| commit | 709005f9a565405d9413eebf8c4ccaff53f0654a (patch) | |
| tree | cd5dc112631ac86fdab5c55696024bd5b7ae3815 /2023/21/21a.c | |
| parent | 477ed667b1a9bc7f73850f45449666a8907ecaed (diff) | |
| download | aoc-709005f9a565405d9413eebf8c4ccaff53f0654a.tar.gz aoc-709005f9a565405d9413eebf8c4ccaff53f0654a.zip | |
Added solutions for 21 and 22
Diffstat (limited to '2023/21/21a.c')
| -rw-r--r-- | 2023/21/21a.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/2023/21/21a.c b/2023/21/21a.c new file mode 100644 index 0000000..a4009d9 --- /dev/null +++ b/2023/21/21a.c | |||
| @@ -0,0 +1,54 @@ | |||
| 1 | #include <stdbool.h> | ||
| 2 | #include <stdio.h> | ||
| 3 | #include <stdlib.h> | ||
| 4 | #include <string.h> | ||
| 5 | |||
| 6 | #define N 200 | ||
| 7 | |||
| 8 | typedef struct { int i, j; } point_t; | ||
| 9 | |||
| 10 | char map[N][N]; | ||
| 11 | int n, nq, nnext; | ||
| 12 | point_t s, q[N*N], nextq[N*N]; | ||
| 13 | |||
| 14 | bool bad(point_t p) { | ||
| 15 | int i = p.i, j = p.j; | ||
| 16 | return i < 0 || j < 0 || i >= n || j >= n || map[i][j] == '#'; | ||
| 17 | } | ||
| 18 | |||
| 19 | void add(point_t p) { | ||
| 20 | if (bad(p)) return; | ||
| 21 | for (int i = 0; i < nnext; i++) | ||
| 22 | if (nextq[i].i == p.i && nextq[i].j == p.j) | ||
| 23 | return; | ||
| 24 | nextq[nnext++] = p; | ||
| 25 | } | ||
| 26 | |||
| 27 | point_t up(point_t p) { return (point_t) {.i = p.i-1, .j = p.j}; } | ||
| 28 | point_t down(point_t p) { return (point_t) {.i = p.i+1, .j = p.j}; } | ||
| 29 | point_t right(point_t p) { return (point_t) {.i = p.i, .j = p.j+1}; } | ||
| 30 | point_t left(point_t p) { return (point_t) {.i = p.i, .j = p.j-1}; } | ||
| 31 | |||
| 32 | int main() { | ||
| 33 | for (n = 0; fgets(map[n], N, stdin) != NULL; n++) | ||
| 34 | for (int i = 0; map[n][i] != '\n'; i++) | ||
| 35 | if (map[n][i] == 'S') | ||
| 36 | s = (point_t) {.i = n, .j = i}; | ||
| 37 | |||
| 38 | int d = 64; | ||
| 39 | add(s); | ||
| 40 | for (int i = 0; i < d; i++) { | ||
| 41 | memcpy(q, nextq, nnext * sizeof(point_t)); | ||
| 42 | nq = nnext; | ||
| 43 | nnext = 0; | ||
| 44 | for (int j = 0; j < nq; j++) { | ||
| 45 | add(up(q[j])); | ||
| 46 | add(down(q[j])); | ||
| 47 | add(right(q[j])); | ||
| 48 | add(left(q[j])); | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | printf("%d\n", nnext); | ||
| 53 | return 0; | ||
| 54 | } | ||
