private static bool[,,,] Cycle4D(bool[,,,] nodes) { bool[,,,] nodes2 = (bool[, , , ])nodes.Clone(); int maxx = nodes.GetLength(0); int maxy = nodes.GetLength(1); int maxz = nodes.GetLength(2); int maxw = nodes.GetLength(2); for (int x = 1; x < maxx - 1; x++) { for (int y = 1; y < maxy - 1; y++) { for (int z = 1; z < maxz - 1; z++) { for (int w = 1; w < maxw - 1; w++) { int count = 0; if (nodes[x - 1, y - 1, z - 1, w - 1]) { count++; } if (nodes[x - 1, y - 1, z - 1, w + 0]) { count++; } if (nodes[x - 1, y - 1, z - 1, w + 1]) { count++; } if (nodes[x - 1, y - 1, z + 0, w - 1]) { count++; } if (nodes[x - 1, y - 1, z + 0, w + 0]) { count++; } if (nodes[x - 1, y - 1, z + 0, w + 1]) { count++; } if (nodes[x - 1, y - 1, z + 1, w - 1]) { count++; } if (nodes[x - 1, y - 1, z + 1, w + 0]) { count++; } if (nodes[x - 1, y - 1, z + 1, w + 1]) { count++; } if (nodes[x - 1, y + 0, z - 1, w - 1]) { count++; } if (nodes[x - 1, y + 0, z - 1, w + 0]) { count++; } if (nodes[x - 1, y + 0, z - 1, w + 1]) { count++; } if (nodes[x - 1, y + 0, z + 0, w - 1]) { count++; } if (nodes[x - 1, y + 0, z + 0, w + 0]) { count++; } if (nodes[x - 1, y + 0, z + 0, w + 1]) { count++; } if (nodes[x - 1, y + 0, z + 1, w - 1]) { count++; } if (nodes[x - 1, y + 0, z + 1, w + 0]) { count++; } if (nodes[x - 1, y + 0, z + 1, w + 1]) { count++; } if (nodes[x - 1, y + 1, z - 1, w - 1]) { count++; } if (nodes[x - 1, y + 1, z - 1, w + 0]) { count++; } if (nodes[x - 1, y + 1, z - 1, w + 1]) { count++; } if (nodes[x - 1, y + 1, z + 0, w - 1]) { count++; } if (nodes[x - 1, y + 1, z + 0, w + 0]) { count++; } if (nodes[x - 1, y + 1, z + 0, w + 1]) { count++; } if (nodes[x - 1, y + 1, z + 1, w - 1]) { count++; } if (nodes[x - 1, y + 1, z + 1, w + 0]) { count++; } if (nodes[x - 1, y + 1, z + 1, w + 1]) { count++; } if (nodes[x + 0, y - 1, z - 1, w - 1]) { count++; } if (nodes[x + 0, y - 1, z - 1, w + 0]) { count++; } if (nodes[x + 0, y - 1, z - 1, w + 1]) { count++; } if (nodes[x + 0, y - 1, z + 0, w - 1]) { count++; } if (nodes[x + 0, y - 1, z + 0, w + 0]) { count++; } if (nodes[x + 0, y - 1, z + 0, w + 1]) { count++; } if (nodes[x + 0, y - 1, z + 1, w - 1]) { count++; } if (nodes[x + 0, y - 1, z + 1, w + 0]) { count++; } if (nodes[x + 0, y - 1, z + 1, w + 1]) { count++; } if (nodes[x + 0, y + 0, z - 1, w - 1]) { count++; } if (nodes[x + 0, y + 0, z - 1, w + 0]) { count++; } if (nodes[x + 0, y + 0, z - 1, w + 1]) { count++; } if (nodes[x + 0, y + 0, z + 0, w - 1]) { count++; } if (nodes[x + 0, y + 0, z + 0, w + 0]) { count++; } if (nodes[x + 0, y + 0, z + 0, w + 1]) { count++; } if (nodes[x + 0, y + 0, z + 1, w - 1]) { count++; } if (nodes[x + 0, y + 0, z + 1, w + 0]) { count++; } if (nodes[x + 0, y + 0, z + 1, w + 1]) { count++; } if (nodes[x + 0, y + 1, z - 1, w - 1]) { count++; } if (nodes[x + 0, y + 1, z - 1, w + 0]) { count++; } if (nodes[x + 0, y + 1, z - 1, w + 1]) { count++; } if (nodes[x + 0, y + 1, z + 0, w - 1]) { count++; } if (nodes[x + 0, y + 1, z + 0, w + 0]) { count++; } if (nodes[x + 0, y + 1, z + 0, w + 1]) { count++; } if (nodes[x + 0, y + 1, z + 1, w - 1]) { count++; } if (nodes[x + 0, y + 1, z + 1, w + 0]) { count++; } if (nodes[x + 0, y + 1, z + 1, w + 1]) { count++; } if (nodes[x + 1, y - 1, z - 1, w - 1]) { count++; } if (nodes[x + 1, y - 1, z - 1, w + 0]) { count++; } if (nodes[x + 1, y - 1, z - 1, w + 1]) { count++; } if (nodes[x + 1, y - 1, z + 0, w - 1]) { count++; } if (nodes[x + 1, y - 1, z + 0, w + 0]) { count++; } if (nodes[x + 1, y - 1, z + 0, w + 1]) { count++; } if (nodes[x + 1, y - 1, z + 1, w - 1]) { count++; } if (nodes[x + 1, y - 1, z + 1, w + 0]) { count++; } if (nodes[x + 1, y - 1, z + 1, w + 1]) { count++; } if (nodes[x + 1, y + 0, z - 1, w - 1]) { count++; } if (nodes[x + 1, y + 0, z - 1, w + 0]) { count++; } if (nodes[x + 1, y + 0, z - 1, w + 1]) { count++; } if (nodes[x + 1, y + 0, z + 0, w - 1]) { count++; } if (nodes[x + 1, y + 0, z + 0, w + 0]) { count++; } if (nodes[x + 1, y + 0, z + 0, w + 1]) { count++; } if (nodes[x + 1, y + 0, z + 1, w - 1]) { count++; } if (nodes[x + 1, y + 0, z + 1, w + 0]) { count++; } if (nodes[x + 1, y + 0, z + 1, w + 1]) { count++; } if (nodes[x + 1, y + 1, z - 1, w - 1]) { count++; } if (nodes[x + 1, y + 1, z - 1, w + 0]) { count++; } if (nodes[x + 1, y + 1, z - 1, w + 1]) { count++; } if (nodes[x + 1, y + 1, z + 0, w - 1]) { count++; } if (nodes[x + 1, y + 1, z + 0, w + 0]) { count++; } if (nodes[x + 1, y + 1, z + 0, w + 1]) { count++; } if (nodes[x + 1, y + 1, z + 1, w - 1]) { count++; } if (nodes[x + 1, y + 1, z + 1, w + 0]) { count++; } if (nodes[x + 1, y + 1, z + 1, w + 1]) { count++; } if (nodes[x, y, z, w]) { nodes2[x, y, z, w] = (count == 2 + 1 || count == 3 + 1); // we count ourselves when active } else { nodes2[x, y, z, w] = (count == 3); } } } } } return(nodes2); }
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); }