aboutsummaryrefslogtreecommitdiff
path: root/2023/22/22a.c
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 /2023/22/22a.c
parent477ed667b1a9bc7f73850f45449666a8907ecaed (diff)
downloadaoc-709005f9a565405d9413eebf8c4ccaff53f0654a.tar.gz
aoc-709005f9a565405d9413eebf8c4ccaff53f0654a.zip
Added solutions for 21 and 22
Diffstat (limited to '2023/22/22a.c')
-rw-r--r--2023/22/22a.c77
1 files changed, 77 insertions, 0 deletions
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}

Generated with cgit - Back to sebastiano.tronto.net