aboutsummaryrefslogtreecommitdiff
path: root/2023
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2023-12-14 19:22:23 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2023-12-14 19:22:23 +0100
commit83adb5e4215a25d17cfc11b4dbf10cf20c418c20 (patch)
tree03dacc46ec973184742d4f112896413c87269b9b /2023
parentb50110f5d370be071f9f1ce5ef4e1a7fdf41ce58 (diff)
downloadaoc-83adb5e4215a25d17cfc11b4dbf10cf20c418c20.tar.gz
aoc-83adb5e4215a25d17cfc11b4dbf10cf20c418c20.zip
Added solution for 14
Diffstat (limited to '2023')
-rw-r--r--2023/14/14a.c20
-rw-r--r--2023/14/14b.c105
2 files changed, 125 insertions, 0 deletions
diff --git a/2023/14/14a.c b/2023/14/14a.c
new file mode 100644
index 0000000..e56a409
--- /dev/null
+++ b/2023/14/14a.c
@@ -0,0 +1,20 @@
1#include <stdio.h>
2
3#define N 102
4
5char line[N];
6int i, j, t, s[N], r[N];
7
8int main() {
9 for (i = 0; fgets(line, N, stdin) != NULL; i++) {
10 for (j = 0; line[j] != '\n'; j++) {
11 if (line[j] == '#') s[j] = i+1;
12 if (line[j] == 'O') r[s[j]++]++;
13 }
14 }
15 for (j = 0; j < i; j++)
16 t += r[j] * (i-j);
17
18 printf("%d\n", t);
19 return 0;
20}
diff --git a/2023/14/14b.c b/2023/14/14b.c
new file mode 100644
index 0000000..9f63057
--- /dev/null
+++ b/2023/14/14b.c
@@ -0,0 +1,105 @@
1#include <inttypes.h>
2#include <stdbool.h>
3#include <stdio.h>
4#include <string.h>
5
6#define N 102
7#define C 10000
8
9uint64_t h[C];
10char p[C][N][N];
11int e, c, n, t, s[N];
12
13void copy_panel(int c1, int c2) {
14 for (int i = 0; i < n; i++)
15 memcpy(p[c2][i], p[c1][i], n+2);
16}
17
18void tilt_north(void) {
19 for (int i = 0; i < n; i++) s[i] = 0;
20 for (int i = 0; i < n; i++) {
21 for (int j = 0; j < n; j++) {
22 if (p[c][i][j] == '#')
23 s[j] = i+1;
24 if (p[c][i][j] == 'O') {
25 if (s[j] != i) p[c][i][j] = '.';
26 p[c][s[j]++][j] = 'O';
27 }
28 }
29 }
30}
31
32void tilt_west(void) {
33 for (int i = 0; i < n; i++) s[i] = 0;
34 for (int j = 0; j < n; j++) {
35 for (int i = 0; i < n; i++) {
36 if (p[c][i][j] == '#')
37 s[i] = j+1;
38 if (p[c][i][j] == 'O') {
39 if (s[i] != j) p[c][i][j] = '.';
40 p[c][i][s[i]++] = 'O';
41 }
42 }
43 }
44}
45
46void tilt_south(void) {
47 for (int i = 0; i < n; i++) s[i] = n-1;
48 for (int i = n-1; i >= 0; i--) {
49 for (int j = 0; j < n; j++) {
50 if (p[c][i][j] == '#')
51 s[j] = i-1;
52 if (p[c][i][j] == 'O') {
53 if (s[j] != i) p[c][i][j] = '.';
54 p[c][s[j]--][j] = 'O';
55 }
56 }
57 }
58}
59
60void tilt_east(void) {
61 for (int i = 0; i < n; i++) s[i] = n-1;
62 for (int j = n-1; j >= 0; j--) {
63 for (int i = 0; i < n; i++) {
64 if (p[c][i][j] == '#')
65 s[i] = j-1;
66 if (p[c][i][j] == 'O') {
67 if (s[i] != j) p[c][i][j] = '.';
68 p[c][i][s[i]--] = 'O';
69 }
70 }
71 }
72}
73
74bool equal(int c1, int c2) {
75 for (int i = 0; i < n; i++)
76 for (int j = 0; j < n; j++)
77 if (p[c1][i][j] != p[c2][i][j])
78 return false;
79 return true;
80}
81
82int main() {
83 for (n = 0; fgets(p[0][n], N, stdin) != NULL; n++) ;
84
85 for (c = 1; c < C; c++) {
86 copy_panel(c-1, c);
87 tilt_north();
88 tilt_west();
89 tilt_south();
90 tilt_east();
91 for (e = 0; e < c; e++)
92 if (equal(e, c))
93 goto found;
94 }
95
96found:
97 c = (1000000000-e) % (c-e) + e;
98
99 for (int i = 0; i < n; i++)
100 for (int j = 0; j < n; j++)
101 t += (p[c][i][j] == 'O') * (n-i);
102
103 printf("%d\n", t);
104 return 0;
105}

Generated with cgit - Back to sebastiano.tronto.net