logo CodeStepByStep logo

ConwayCubes

Write a method named ConwayCubes that simulates a 3-D version of the classic Conway's Game of Life. Your method processes a file of Your method accepts as its parameter a string representing the name of a file of Conway cube data.

Think of the game as being an infinite 3-dimensional grid. At every integer 3-dimensional coordinate (x,y,z), there exists a single cube that is either active or inactive.

In the initial state of the pocket dimension, almost all cubes start inactive. The only exception is a small flat region of cubes, your puzzle input; the cubes in this region start in the specified active (#) or inactive (.) state depending on the file input. Your task is to read the file input and then simulate 6 cycles of the game.

On a given cycle, each cube considers its neighbors: any of the 26 other cubes where any of their x,y,z coordinates differ by at most 1. For example, given the cube at x=1,y=2,z=3, its neighbors include the cube at x=2,y=2,z=2, the cube at x=0,y=2,z=3, and so on.

During a cycle, all cubes simultaneously change their state according to the following rules:

  • If a cube is active and exactly 2 or 3 of its neighbors are also active, the cube remains active. Otherwise, the cube becomes inactive.
  • If a cube is inactive but exactly 3 of its neighbors are active, the cube becomes active. Otherwise, the cube remains inactive.

For example, consider the following game data, stored in a file named life.txt:

.#.
..#
###

This initial state represents a small 3x3x1 slice of the infinite 3-dimensional space. Simulating a few cycles from this initial state produces the following configurations, where the result of each cycle is shown layer-by-layer at each given z coordinate (and the frame of view follows the active cells in each cycle):

Before any cycles:
z=0
.#.
..#
###
After 1 cycle:
z=-1
#..
..#
.#.
z=0
#.#
.##
.#.
z=1
#..
..#
.#.
After 2 cycles:
z=-2
.....
.....
..#..
.....
.....
z=-1
..#..
.#..#
....#
.#...
.....
z=0
##...
##...
#....
....#
.###.
z=1
..#..
.#..#
....#
.#...
.....
z=2
.....
.....
..#..
.....
.....
After 3 cycles:
z=-2
.......
.......
..##...
..###..
.......
.......
.......
z=-1
..#....
...#...
#......
.....##
.#...#.
..#.#..
...#...
z=0
...#...
.......
#......
.......
.....##
.##.#..
...#...
z=1
..#....
...#...
#......
.....##
.#...#.
..#.#..
...#...
z=2
.......
.......
..##...
..###..
.......
.......
.......

For this example, after the full six-cycle boot process completes, 112 cubes are left in the active state. So the call of ConwayCubes("life.txt") would return 112.

You may assume that the file exists and is readable, that it follows the format described above, and that there will be at least one row and column in the board.

(This exercise is based on the Advent of Code 2020, day 17.)

Method: Write a C# method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.