diff options
author | June McEnroe <june@causal.agency> | 2020-12-11 00:18:12 -0500 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2020-12-11 00:18:12 -0500 |
commit | 76e74a35bf916da3d1687b6fc2a4c4026c4bdf2e (patch) | |
tree | c68d78036c5a84b36adbe63da2599900194c80e8 /2020 | |
parent | Solve day 10 part 2 (diff) | |
download | aoc-76e74a35bf916da3d1687b6fc2a4c4026c4bdf2e.tar.gz aoc-76e74a35bf916da3d1687b6fc2a4c4026c4bdf2e.zip |
Solve day 11 part 1
Diffstat (limited to '')
-rw-r--r-- | 2020/day11.c | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/2020/day11.c b/2020/day11.c new file mode 100644 index 0000000..f8552ca --- /dev/null +++ b/2020/day11.c @@ -0,0 +1,58 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +struct Grid { + int h, w; + char c[128][128]; +}; +static char at(const struct Grid *grid, int y, int x) { + if (x < 0 || y < 0 || x >= grid->w || y >= grid->h) return '.'; + return grid->c[y][x]; +} +static int adj(const struct Grid *grid, int y, int x) { + return ( + (at(grid, y - 1, x - 1) == '#') + + (at(grid, y - 1, x + 0) == '#') + + (at(grid, y - 1, x + 1) == '#') + + (at(grid, y + 0, x - 1) == '#') + + (at(grid, y + 0, x + 1) == '#') + + (at(grid, y + 1, x - 1) == '#') + + (at(grid, y + 1, x + 0) == '#') + + (at(grid, y + 1, x + 1) == '#') + ); +} +static struct Grid gen(const struct Grid *prev) { + struct Grid next = { .h = prev->h, .w = prev->w }; + for (int y = 0; y < next.h; ++y) { + for (int x = 0; x < next.w; ++x) { + if (prev->c[y][x] == 'L' && !adj(prev, y, x)) { + next.c[y][x] = '#'; + } else if (prev->c[y][x] == '#' && adj(prev, y, x) >= 4) { + next.c[y][x] = 'L'; + } else { + next.c[y][x] = prev->c[y][x]; + } + } + } + return next; +} +int main(void) { + struct Grid init = {0}; + while (EOF != scanf("%s\n", init.c[init.h])) { + init.h++; + } + init.w = strlen(init.c[0]); + struct Grid prev = init; + for (;;) { + struct Grid next = gen(&prev); + if (!memcmp(&next, &prev, sizeof(next))) break; + prev = next; + } + int occ = 0; + for (int y = 0; y < prev.h; ++y) { + for (int x = 0; x < prev.w; ++x) { + occ += prev.c[y][x] == '#'; + } + } + printf("%d\n", occ); +} |