From dac6ebc34d0c577d31c604450c66a49c5b34df25 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Thu, 17 Dec 2020 13:56:57 -0500 Subject: Solve day 17 part 1 --- 2020/day17.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 2020/day17.c 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 +#include +#include +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); +} -- cgit 1.4.1