diff options
author | June McEnroe <june@causal.agency> | 2020-12-17 13:56:57 -0500 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2020-12-17 13:56:57 -0500 |
commit | dac6ebc34d0c577d31c604450c66a49c5b34df25 (patch) | |
tree | 52d7c3d38df559b134abcdb019744e963a0f942e /2020 | |
parent | Solve day 16 part 2 (diff) | |
download | aoc-dac6ebc34d0c577d31c604450c66a49c5b34df25.tar.gz aoc-dac6ebc34d0c577d31c604450c66a49c5b34df25.zip |
Solve day 17 part 1
Diffstat (limited to '')
-rw-r--r-- | 2020/day17.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/2020/day17.c b/2020/day17.c new file mode 100644 index 0000000..6cab278 --- /dev/null +++ b/2020/day17.c @@ -0,0 +1,48 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +struct Dim { + char _[32][32][32]; +}; +static int neighbors(const struct Dim *d, int z, int y, int x) { + int active = 0; + for (int i = -1; i <= +1; ++i) + for (int j = -1; j <= +1; ++j) + for (int k = -1; k <= +1; ++k) { + if (!i && !j && !k) continue; + active += (d->_[z+i&31][y+j&31][x+k&31] == '#'); + } + return active; +} +static struct Dim step(const struct Dim *prev) { + struct Dim next; + for (int z = 0; z < 32; ++z) + for (int y = 0; y < 32; ++y) + for (int x = 0; x < 32; ++x) { + int active = neighbors(prev, z, y, x); + if (prev->_[z][y][x] == '#') { + next._[z][y][x] = (active == 2 || active == 3 ? '#' : '.'); + } else { + next._[z][y][x] = (active == 3 ? '#' : '.'); + } + } + return next; +} +int main(void) { + struct Dim init = {0}; + for (int y = 0;; ++y) { + if (scanf("%s\n", init._[0][y]) < 1) break; + } + struct Dim prev = init; + for (int i = 0; i < 6; ++i) { + struct Dim next = step(&prev); + prev = next; + } + int active = 0; + for (int z = 0; z < 32; ++z) + for (int y = 0; y < 32; ++y) + for (int x = 0; x < 32; ++x) { + active += (prev._[z][y][x] == '#'); + } + printf("%d\n", active); +} |