diff options
Diffstat (limited to '2023/21')
| -rw-r--r-- | 2023/21/21a.c | 54 | ||||
| -rw-r--r-- | 2023/21/21b.c | 83 | ||||
| -rw-r--r-- | 2023/21/fuckyou.png | bin | 0 -> 544 bytes | |||
| -rw-r--r-- | 2023/21/fuckyou2.png | bin | 0 -> 523 bytes |
4 files changed, 137 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 | } | ||
diff --git a/2023/21/21b.c b/2023/21/21b.c new file mode 100644 index 0000000..1748185 --- /dev/null +++ b/2023/21/21b.c | |||
| @@ -0,0 +1,83 @@ | |||
| 1 | /* | ||
| 2 | This is another problem I did not enjoy. Once again, it was only solvable | ||
| 3 | because of the carefully crafted input case. This makes the problem | ||
| 4 | solvable, but at the same time it leaves you wondering - which unsaid | ||
| 5 | assumptions are true and which are not? | ||
| 6 | |||
| 7 | For example, in my first 14 attempts or so I assumed that every non-# cell | ||
| 8 | was reached at the same time as if there were no #-cells. A reasonable | ||
| 9 | assumption, and almost true. Unfortunately there were some cells that | ||
| 10 | were completely enclosed by #-cells, and thus unreachable. See fuckyou.png | ||
| 11 | and fuckyou2.png. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include <inttypes.h> | ||
| 15 | #include <stdbool.h> | ||
| 16 | #include <stdio.h> | ||
| 17 | #include <stdlib.h> | ||
| 18 | #include <string.h> | ||
| 19 | |||
| 20 | #define N 200 | ||
| 21 | #define S 202300L /* My input was 202300*131+65 */ | ||
| 22 | |||
| 23 | char map[N][N]; | ||
| 24 | int64_t n; | ||
| 25 | |||
| 26 | int go(char m[][N], int i, int j) { | ||
| 27 | if (i >= 0 && j >= 0 && i < n && j < n && m[i][j] == '.') { | ||
| 28 | m[i][j] = 'S'; | ||
| 29 | return 1; | ||
| 30 | } | ||
| 31 | return 0; | ||
| 32 | } | ||
| 33 | |||
| 34 | int64_t flood(int64_t x, int64_t y, int64_t steps) { | ||
| 35 | int64_t c, i, j; | ||
| 36 | char tmp[N][N]; | ||
| 37 | for (int i = 0; i < n; i++) memcpy(tmp[i], map[i], n*sizeof(char)); | ||
| 38 | tmp[x][y] = 'S'; | ||
| 39 | for (int s = 0; s < steps; s++) { | ||
| 40 | for (i = 0, c = 0; i < n; i++) { | ||
| 41 | for (j = (s+x+y+i)%2; j < n; j += 2) { | ||
| 42 | if (tmp[i][j] == 'S') { | ||
| 43 | tmp[i][j] = '.'; | ||
| 44 | c += go(tmp, i-1, j) + | ||
| 45 | go(tmp, i+1, j) + | ||
| 46 | go(tmp, i, j-1) + | ||
| 47 | go(tmp, i, j+1); | ||
| 48 | } | ||
| 49 | } | ||
| 50 | } | ||
| 51 | } | ||
| 52 | return c; | ||
| 53 | } | ||
| 54 | |||
| 55 | int main() { | ||
| 56 | for (n = 0; fgets(map[n], N, stdin) != NULL; n++) ; | ||
| 57 | map[n/2][n/2] = '.'; | ||
| 58 | |||
| 59 | int64_t fulleven = flood(n/2, n/2, n-1); | ||
| 60 | int64_t fullodd = flood(n/2, n/2, n); | ||
| 61 | int64_t arrowr = flood(n/2, 0, n-1); | ||
| 62 | int64_t arrowl = flood(n/2, n-1, n-1); | ||
| 63 | int64_t arrowd = flood(0, n/2, n-1); | ||
| 64 | int64_t arrowu = flood(n-1, n/2, n-1); | ||
| 65 | int64_t topl = flood(0, 0, n/2-1); | ||
| 66 | int64_t topr = flood(0, n-1, n/2-1); | ||
| 67 | int64_t botl = flood(n-1, 0, n/2-1); | ||
| 68 | int64_t botr = flood(n-1, n-1, n/2-1); | ||
| 69 | int64_t cutbr = flood(0, 0, n/2+n-1); | ||
| 70 | int64_t cutbl = flood(0, n-1, n/2+n-1); | ||
| 71 | int64_t cuttr = flood(n-1, 0, n/2+n-1); | ||
| 72 | int64_t cuttl = flood(n-1, n-1, n/2+n-1); | ||
| 73 | |||
| 74 | int64_t fulls = S*S*fulleven + (S-1)*(S-1)*fullodd; | ||
| 75 | int64_t smallborder = S*(topl + topr + botl + botr); | ||
| 76 | int64_t bigborder = (S-1)*(cutbr + cutbl + cuttr + cuttl); | ||
| 77 | int64_t arrows = arrowr + arrowl + arrowu + arrowd; | ||
| 78 | int64_t final = fulls + smallborder + bigborder + arrows; | ||
| 79 | |||
| 80 | printf("%" PRId64 "\n", final); | ||
| 81 | |||
| 82 | return 0; | ||
| 83 | } | ||
diff --git a/2023/21/fuckyou.png b/2023/21/fuckyou.png new file mode 100644 index 0000000..4fb5ddc --- /dev/null +++ b/2023/21/fuckyou.png | |||
| Binary files differ | |||
diff --git a/2023/21/fuckyou2.png b/2023/21/fuckyou2.png new file mode 100644 index 0000000..2885148 --- /dev/null +++ b/2023/21/fuckyou2.png | |||
| Binary files differ | |||
