summary refs log tree commit diff homepage
diff options
context:
space:
mode:
-rw-r--r--2021/day13.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/2021/day13.c b/2021/day13.c
new file mode 100644
index 0000000..9635616
--- /dev/null
+++ b/2021/day13.c
@@ -0,0 +1,50 @@
+#include <stdio.h>
+#include <stdlib.h>
+static int h, w;
+static int paper[2048][2048];
+static void foldY(int fold) {
+	for (int y = 0; y < fold; ++y)
+	for (int x = 0; x < w; ++x) {
+		paper[y][x] |= paper[h-1-y][x];
+	}
+	h = fold;
+}
+static void foldX(int fold) {
+	for (int x = 0; x < fold; ++x)
+	for (int y = 0; y < h; ++y) {
+		paper[y][x] |= paper[y][w-1-x];
+	}
+	w = fold;
+}
+static int sum(void) {
+	int sum = 0;
+	for (int y = 0; y < h; ++y)
+	for (int x = 0; x < w; ++x) {
+		sum += paper[y][x];
+	}
+	return sum;
+}
+int main(void) {
+	int x, y;
+	while (2 == scanf("%d,%d\n", &x, &y)) {
+		paper[y][x] = 1;
+		if (y >= h) h = y+1;
+		if (x >= w) w = x+1;
+	}
+	char axis;
+	int fold;
+	int first = 1;
+	while (EOF != scanf(" fold along %c=%d\n", &axis, &fold)) {
+		if (axis == 'y') {
+			foldY(fold);
+		} else if (axis == 'x') {
+			foldX(fold);
+		} else {
+			printf("??? %c\n", axis);
+		}
+		if (first) {
+			printf("%d\n", sum());
+			first = 0;
+		}
+	}
+}