diff options
author | June McEnroe <june@causal.agency> | 2020-12-17 14:04:56 -0500 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2020-12-17 14:04:56 -0500 |
commit | e06b2d1c4fcbafecc66b0992f0264153f1f062d6 (patch) | |
tree | 3e7503e944fce42860563320ae44605bd0734095 | |
parent | Solve day 17 part 1 (diff) | |
download | aoc-e06b2d1c4fcbafecc66b0992f0264153f1f062d6.tar.gz aoc-e06b2d1c4fcbafecc66b0992f0264153f1f062d6.zip |
Solve day 17 part 2
-rw-r--r-- | 2020/day17.c | 66 |
1 files changed, 55 insertions, 11 deletions
diff --git a/2020/day17.c b/2020/day17.c index 6cab278..620563f 100644 --- a/2020/day17.c +++ b/2020/day17.c @@ -1,10 +1,10 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -struct Dim { +struct Dim3 { char _[32][32][32]; }; -static int neighbors(const struct Dim *d, int z, int y, int x) { +static int neighbors3(const struct Dim3 *d, int z, int y, int x) { int active = 0; for (int i = -1; i <= +1; ++i) for (int j = -1; j <= +1; ++j) @@ -14,12 +14,12 @@ static int neighbors(const struct Dim *d, int z, int y, int x) { } return active; } -static struct Dim step(const struct Dim *prev) { - struct Dim next; +static struct Dim3 step3(const struct Dim3 *prev) { + struct Dim3 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); + int active = neighbors3(prev, z, y, x); if (prev->_[z][y][x] == '#') { next._[z][y][x] = (active == 2 || active == 3 ? '#' : '.'); } else { @@ -28,21 +28,65 @@ static struct Dim step(const struct Dim *prev) { } return next; } +struct Dim4 { + char _[32][32][32][32]; +}; +static int neighbors4(const struct Dim4 *d, int w, 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) + for (int l = -1; l <= +1; ++l) { + if (!i && !j && !k && !l) continue; + active += (d->_[w+i&31][z+j&31][y+k&31][x+l&31] == '#'); + } + return active; +} +static struct Dim4 step4(const struct Dim4 *prev) { + struct Dim4 next; + for (int w = 0; w < 32; ++w) + for (int z = 0; z < 32; ++z) + for (int y = 0; y < 32; ++y) + for (int x = 0; x < 32; ++x) { + int active = neighbors4(prev, w, z, y, x); + if (prev->_[w][z][y][x] == '#') { + next._[w][z][y][x] = (active == 2 || active == 3 ? '#' : '.'); + } else { + next._[w][z][y][x] = (active == 3 ? '#' : '.'); + } + } + return next; +} int main(void) { - struct Dim init = {0}; + struct Dim3 init3 = {0}; + struct Dim4 init4 = {0}; for (int y = 0;; ++y) { - if (scanf("%s\n", init._[0][y]) < 1) break; + if (scanf("%s\n", init3._[0][y]) < 1) break; + strcpy(init4._[0][0][y], init3._[0][y]); } - struct Dim prev = init; + struct Dim3 prev3 = init3; for (int i = 0; i < 6; ++i) { - struct Dim next = step(&prev); - prev = next; + struct Dim3 next3 = step3(&prev3); + prev3 = next3; } 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] == '#'); + active += (prev3._[z][y][x] == '#'); + } + printf("%d\n", active); + struct Dim4 prev4 = init4; + for (int i = 0; i < 6; ++i) { + struct Dim4 next4 = step4(&prev4); + prev4 = next4; + } + active = 0; + for (int w = 0; w < 32; ++w) + for (int z = 0; z < 32; ++z) + for (int y = 0; y < 32; ++y) + for (int x = 0; x < 32; ++x) { + active += (prev4._[w][z][y][x] == '#'); } printf("%d\n", active); } |