about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--aoc/2024/05/part-one.c27
-rw-r--r--aoc/2024/05/part-two.c35
2 files changed, 62 insertions, 0 deletions
diff --git a/aoc/2024/05/part-one.c b/aoc/2024/05/part-one.c
new file mode 100644
index 0000000..4d2b560
--- /dev/null
+++ b/aoc/2024/05/part-one.c
@@ -0,0 +1,27 @@
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+int main()
+{
+	bool after[100][100] = {false};
+	unsigned char x, y;
+	while (scanf("%hhu|%hhu\n", &x, &y) == 2)
+		after[y][x] = true;
+
+	unsigned char line[90];
+	unsigned sum = y = 0;
+	do {
+		bool right = true;
+		do {
+			line[y] = x;
+			for (unsigned char i = 0; right && i < y; ++i)
+				if (after[line[i]][line[y]])
+					right = false;
+		} while (scanf(",%hhu", &x) == 1 && ++y);
+		if (right)
+			sum += line[y + 1 >> 1];
+	} while (y = 0, scanf("%hhu", &x) == 1);
+	printf("%u\n", sum);
+	return 0;
+}
diff --git a/aoc/2024/05/part-two.c b/aoc/2024/05/part-two.c
new file mode 100644
index 0000000..443e8b0
--- /dev/null
+++ b/aoc/2024/05/part-two.c
@@ -0,0 +1,35 @@
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+static bool after[100][100];
+
+int compare(const void *x, const void *y)
+{
+	return after[*(const unsigned char *) x][*(const unsigned char *) y];
+}
+
+int main()
+{
+	unsigned char x, y;
+	while (scanf("%hhu|%hhu\n", &x, &y) == 2)
+		after[y][x] = true;
+
+	unsigned char line[90];
+	unsigned sum = y = 0;
+	do {
+		bool right = true;
+		do {
+			line[y] = x;
+			for (unsigned char i = 0; right && i < y; ++i)
+				if (after[line[i]][line[y]])
+					right = false;
+		} while (scanf(",%hhu", &x) == 1 && ++y);
+		if (right)
+			continue;
+		qsort(line, ++y, 1, compare);
+		sum += line[y >> 1];
+	} while (y = 0, scanf("%hhu", &x) == 1);
+	printf("%u\n", sum);
+	return 0;
+}