aoc

My solutions for the Advent of Code
git clone https://git.tronto.net/aoc
Download | Log | Files | Refs | README

commit 912df267a6367396fa0f8ed1fa3700e0948a40cc
parent 5ad6393c60da6a2b2c5c4a796f58a3fc05ed8db9
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date:   Thu, 14 Dec 2023 19:22:23 +0100

Added solution for 14

Diffstat:
A2023/14/14a.c | 20++++++++++++++++++++
A2023/14/14b.c | 105+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 125 insertions(+), 0 deletions(-)

diff --git a/2023/14/14a.c b/2023/14/14a.c @@ -0,0 +1,20 @@ +#include <stdio.h> + +#define N 102 + +char line[N]; +int i, j, t, s[N], r[N]; + +int main() { + for (i = 0; fgets(line, N, stdin) != NULL; i++) { + for (j = 0; line[j] != '\n'; j++) { + if (line[j] == '#') s[j] = i+1; + if (line[j] == 'O') r[s[j]++]++; + } + } + for (j = 0; j < i; j++) + t += r[j] * (i-j); + + printf("%d\n", t); + return 0; +} diff --git a/2023/14/14b.c b/2023/14/14b.c @@ -0,0 +1,105 @@ +#include <inttypes.h> +#include <stdbool.h> +#include <stdio.h> +#include <string.h> + +#define N 102 +#define C 10000 + +uint64_t h[C]; +char p[C][N][N]; +int e, c, n, t, s[N]; + +void copy_panel(int c1, int c2) { + for (int i = 0; i < n; i++) + memcpy(p[c2][i], p[c1][i], n+2); +} + +void tilt_north(void) { + for (int i = 0; i < n; i++) s[i] = 0; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + if (p[c][i][j] == '#') + s[j] = i+1; + if (p[c][i][j] == 'O') { + if (s[j] != i) p[c][i][j] = '.'; + p[c][s[j]++][j] = 'O'; + } + } + } +} + +void tilt_west(void) { + for (int i = 0; i < n; i++) s[i] = 0; + for (int j = 0; j < n; j++) { + for (int i = 0; i < n; i++) { + if (p[c][i][j] == '#') + s[i] = j+1; + if (p[c][i][j] == 'O') { + if (s[i] != j) p[c][i][j] = '.'; + p[c][i][s[i]++] = 'O'; + } + } + } +} + +void tilt_south(void) { + for (int i = 0; i < n; i++) s[i] = n-1; + for (int i = n-1; i >= 0; i--) { + for (int j = 0; j < n; j++) { + if (p[c][i][j] == '#') + s[j] = i-1; + if (p[c][i][j] == 'O') { + if (s[j] != i) p[c][i][j] = '.'; + p[c][s[j]--][j] = 'O'; + } + } + } +} + +void tilt_east(void) { + for (int i = 0; i < n; i++) s[i] = n-1; + for (int j = n-1; j >= 0; j--) { + for (int i = 0; i < n; i++) { + if (p[c][i][j] == '#') + s[i] = j-1; + if (p[c][i][j] == 'O') { + if (s[i] != j) p[c][i][j] = '.'; + p[c][i][s[i]--] = 'O'; + } + } + } +} + +bool equal(int c1, int c2) { + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + if (p[c1][i][j] != p[c2][i][j]) + return false; + return true; +} + +int main() { + for (n = 0; fgets(p[0][n], N, stdin) != NULL; n++) ; + + for (c = 1; c < C; c++) { + copy_panel(c-1, c); + tilt_north(); + tilt_west(); + tilt_south(); + tilt_east(); + for (e = 0; e < c; e++) + if (equal(e, c)) + goto found; + } + +found: + c = (1000000000-e) % (c-e) + e; + + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + t += (p[c][i][j] == 'O') * (n-i); + + printf("%d\n", t); + return 0; +}