private static int CountOccupiedAdjacentSlots(bool[,,,] space, int slotX, int slotY, int slotZ, int slotF) { int count = 0; for (int f = slotF - 1; f <= slotF + 1; f++) { for (int z = slotZ - 1; z <= slotZ + 1; z++) { for (int x = slotX - 1; x <= slotX + 1; x++) { for (int y = slotY - 1; y <= slotY + 1; y++) { if ( !(x == slotX && y == slotY && z == slotZ && f == slotF) && f >= 0 && f <= space.GetUpperBound(0) && z >= 0 && z <= space.GetUpperBound(1) && x >= 0 && x <= space.GetUpperBound(2) && y >= 0 && y <= space.GetUpperBound(3) ) { if (space[f, z, x, y] == true) { count++; } } } } } } return(count); }
static int CountNeighbors4D(bool[,,,] quad, int w, int z, int y, int x) { int found = 0; int neighb = 0; for (var l = Math.Max(0, w - 1); Math.Min(w + 1, quad.GetUpperBound(0)) >= l; ++l) { for (var k = Math.Max(0, z - 1); Math.Min(z + 1, quad.GetUpperBound(1)) >= k; ++k) { for (var j = Math.Max(0, y - 1); Math.Min(y + 1, quad.GetUpperBound(2)) >= j; ++j) { for (var i = Math.Max(0, x - 1); Math.Min(x + 1, quad.GetUpperBound(3)) >= i; ++i) { if (w == l && z == k && y == j && x == i) { continue; } neighb++; if (quad[l, k, j, i]) { found++; } } } } } return(found); }
static void DumpZPlane4D(bool[,,,] quad, int plane, int w) { for (var j = 0; quad.GetUpperBound(0) >= j; ++j) { for (var i = 0; quad.GetUpperBound(1) >= i; ++i) { Debug.Write(quad[w, plane, j, i] ? '#' : '.'); } Debug.Write("\n"); } }
private static int Solve4D(bool[,,,] space) { int iterations = 0; bool[,,,] spaceShadow = (bool[, , , ])space.Clone(); do { bool[,,,] spaceCopy = (bool[, , , ])spaceShadow.Clone(); for (int f = 0; f <= spaceCopy.GetUpperBound(0); f++) { for (int z = 0; z <= spaceCopy.GetUpperBound(1); z++) { for (int x = 0; x <= spaceCopy.GetUpperBound(2); x++) { for (int y = 0; y <= spaceCopy.GetUpperBound(3); y++) { bool isOccupied = spaceCopy[f, z, x, y]; int occupiedAround = CountOccupiedAdjacentSlots(spaceCopy, x, y, z, f); if (isOccupied == false) { spaceShadow[f, z, x, y] = occupiedAround == 3; } if (isOccupied == true) { spaceShadow[f, z, x, y] = occupiedAround >= 2 && occupiedAround <= 3; } } } } } iterations++; // Debug //for (int z = 0; z <= spaceShadow.GetUpperBound(0); z++) //{ // Console.WriteLine("\n----"); // Console.WriteLine(z); // Console.Write("----"); // for (int x = 0; x <= spaceShadow.GetUpperBound(1); x++) // { // Console.Write("\n"); // for (int y = 0; y <= spaceShadow.GetUpperBound(2); y++) // { // if (spaceShadow[z, x, y] == true) // { // Console.Write('#'); // } // else // { // Console.Write('.'); // } // } // } //} } while (iterations < CYCLES); int occupiedSlots = 0; for (int f = 0; f <= spaceShadow.GetUpperBound(0); f++) { for (int z = 0; z <= spaceShadow.GetUpperBound(1); z++) { for (int x = 0; x <= spaceShadow.GetUpperBound(2); x++) { for (int y = 0; y <= spaceShadow.GetUpperBound(3); y++) { if (spaceShadow[f, z, x, y] == true) { occupiedSlots++; } } } } } return(occupiedSlots); }