#include #include #include struct Dim3 { char _[32][32][32]; }; 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) 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 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 = neighbors3(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; } 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 Dim3 init3 = {0}; struct Dim4 init4 = {0}; for (int y = 0;; ++y) { if (scanf("%s\n", init3._[0][y]) < 1) break; strcpy(init4._[0][0][y], init3._[0][y]); } struct Dim3 prev3 = init3; for (int i = 0; i < 6; ++i) { 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 += (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); }