aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2023-12-22 16:44:48 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2023-12-22 16:44:48 +0100
commit709005f9a565405d9413eebf8c4ccaff53f0654a (patch)
treecd5dc112631ac86fdab5c55696024bd5b7ae3815
parent477ed667b1a9bc7f73850f45449666a8907ecaed (diff)
downloadaoc-709005f9a565405d9413eebf8c4ccaff53f0654a.tar.gz
aoc-709005f9a565405d9413eebf8c4ccaff53f0654a.zip
Added solutions for 21 and 22
-rw-r--r--2023/21/21a.c54
-rw-r--r--2023/21/21b.c83
-rw-r--r--2023/21/fuckyou.pngbin0 -> 544 bytes
-rw-r--r--2023/21/fuckyou2.pngbin0 -> 523 bytes
-rw-r--r--2023/22/22a.c77
-rw-r--r--2023/22/22b.c85
6 files changed, 299 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
8typedef struct { int i, j; } point_t;
9
10char map[N][N];
11int n, nq, nnext;
12point_t s, q[N*N], nextq[N*N];
13
14bool 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
19void 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
27point_t up(point_t p) { return (point_t) {.i = p.i-1, .j = p.j}; }
28point_t down(point_t p) { return (point_t) {.i = p.i+1, .j = p.j}; }
29point_t right(point_t p) { return (point_t) {.i = p.i, .j = p.j+1}; }
30point_t left(point_t p) { return (point_t) {.i = p.i, .j = p.j-1}; }
31
32int 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/*
2This is another problem I did not enjoy. Once again, it was only solvable
3because of the carefully crafted input case. This makes the problem
4solvable, but at the same time it leaves you wondering - which unsaid
5assumptions are true and which are not?
6
7For example, in my first 14 attempts or so I assumed that every non-# cell
8was reached at the same time as if there were no #-cells. A reasonable
9assumption, and almost true. Unfortunately there were some cells that
10were completely enclosed by #-cells, and thus unreachable. See fuckyou.png
11and 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
23char map[N][N];
24int64_t n;
25
26int 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
34int64_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
55int 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
diff --git a/2023/22/22a.c b/2023/22/22a.c
new file mode 100644
index 0000000..a70a181
--- /dev/null
+++ b/2023/22/22a.c
@@ -0,0 +1,77 @@
1#include <inttypes.h>
2#include <stdbool.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6
7#define N 1500
8#define M 340
9#define MAX(x,y) ((x)>(y)?(x):(y))
10
11#define isnum(c) (c == '-' || (c >= '0' && c <= '9'))
12
13char line[N];
14int n, x, b[N][6], cube[M][M][M];
15
16void readl(int nums[], char *buf) {
17 for (int i = 0; i < 6; i++) {
18 nums[i] = atoi(buf);
19 while (isnum(*buf)) buf++;
20 buf++;
21 }
22}
23
24bool droppable(int i) {
25 int z = b[i][2];
26 if (z == 1) return false;
27 for (int x = b[i][0]; x <= b[i][3]; x++)
28 for (int y = b[i][1]; y <= b[i][4]; y++)
29 if (cube[x][y][z-1])
30 return false;
31 return true;
32}
33
34void set(int n, int c) {
35 for (int i = b[n][0]; i <= b[n][3]; i++)
36 for (int j = b[n][1]; j <= b[n][4]; j++)
37 for (int k = b[n][2]; k <= b[n][5]; k++)
38 cube[i][j][k] = c;
39}
40
41void dobreak(int n) { set(n, 0); }
42void unbreak(int n) { set(n, n+1); }
43
44void drop(int i) {
45 dobreak(i);
46 b[i][2]--;
47 b[i][5]--;
48 unbreak(i);
49}
50
51int disintegratable(int i) {
52 int ret = 1;
53 dobreak(i);
54 for (int j = 0; j < n; j++)
55 if (j != i && droppable(j))
56 ret = 0;
57 unbreak(i);
58 return ret;
59}
60
61int main() {
62 for (n = 0; fgets(line, N, stdin) != NULL; n++) {
63 readl(b[n], line);
64 unbreak(n);
65 }
66
67 for (int i = 0; i < N; i++)
68 for (int j = 0; j < n; j++)
69 while (droppable(j))
70 drop(j);
71
72 for (int i = 0; i < n; i++)
73 x += disintegratable(i);
74
75 printf("%d\n", x);
76 return 0;
77}
diff --git a/2023/22/22b.c b/2023/22/22b.c
new file mode 100644
index 0000000..bbafdfa
--- /dev/null
+++ b/2023/22/22b.c
@@ -0,0 +1,85 @@
1#include <inttypes.h>
2#include <stdbool.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6
7#define N 1500
8#define M 340
9#define MAX(x,y) ((x)>(y)?(x):(y))
10
11#define isnum(c) (c == '-' || (c >= '0' && c <= '9'))
12
13char line[N];
14int n, x, b[N][6], cube[M][M][M];
15
16void readl(int nums[], char *buf) {
17 for (int i = 0; i < 6; i++) {
18 nums[i] = atoi(buf);
19 while (isnum(*buf)) buf++;
20 buf++;
21 }
22}
23
24bool droppable(int i) {
25 int z = b[i][2];
26 if (z == 1) return false;
27 if (!cube[b[i][0]][b[i][1]][b[i][2]]) return false; /* Already dropping */
28 for (int x = b[i][0]; x <= b[i][3]; x++)
29 for (int y = b[i][1]; y <= b[i][4]; y++)
30 if (cube[x][y][z-1])
31 return false;
32 return true;
33}
34
35void set(int n, int c) {
36 for (int i = b[n][0]; i <= b[n][3]; i++)
37 for (int j = b[n][1]; j <= b[n][4]; j++)
38 for (int k = b[n][2]; k <= b[n][5]; k++)
39 cube[i][j][k] = c;
40}
41
42void dobreak(int n) { set(n, 0); }
43void unbreak(int n) { set(n, n+1); }
44
45void drop(int i) {
46 dobreak(i);
47 b[i][2]--;
48 b[i][5]--;
49 unbreak(i);
50}
51
52int howmanyfall(int i) {
53 int k = 0, q[N];
54 q[k++] = i;
55 dobreak(i);
56 for (int t = 0; t < k; t++) {
57 for (int j = 0; j < n; j++) {
58 if (j != q[t] && droppable(j)) {
59 q[k++] = j;
60 dobreak(j);
61 }
62 }
63 }
64 for (int s = 0; s < k; s++)
65 unbreak(q[s]);
66 return k-1;
67}
68
69int main() {
70 for (n = 0; fgets(line, N, stdin) != NULL; n++) {
71 readl(b[n], line);
72 unbreak(n);
73 }
74
75 for (int i = 0; i < N; i++)
76 for (int j = 0; j < n; j++)
77 while (droppable(j))
78 drop(j);
79
80 for (int i = 0; i < n; i++)
81 x += homanyfall(i);
82
83 printf("%d\n", x);
84 return 0;
85}

Generated with cgit - Back to sebastiano.tronto.net