diff options
author | June McEnroe <june@causal.agency> | 2021-12-13 11:08:53 -0500 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2021-12-13 11:08:53 -0500 |
commit | 955eb47404e076995fadf2d2cc83d9a43a01dfc2 (patch) | |
tree | a0cbc6a2a7598e92f20296cb97c5c35c0c609782 /2021 | |
parent | Pop instead of shift (diff) | |
download | aoc-955eb47404e076995fadf2d2cc83d9a43a01dfc2.tar.gz aoc-955eb47404e076995fadf2d2cc83d9a43a01dfc2.zip |
Solve day 13 part 1
Diffstat (limited to '2021')
-rw-r--r-- | 2021/day13.c | 50 |
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; + } + } +} |