From 955eb47404e076995fadf2d2cc83d9a43a01dfc2 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Mon, 13 Dec 2021 11:08:53 -0500 Subject: Solve day 13 part 1 --- 2021/day13.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 2021/day13.c (limited to '2021') 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 +#include +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; + } + } +} -- cgit 1.4.1