Ejemplo n.º 1
0
        public long CountNumberOfD3Cubes()
        {
            for (var iteration = 0; iteration < 6; iteration++)
            {
                IterateCubes();
            }

            return(D3Cubes.Count(x => x.IsActive));
        }
Ejemplo n.º 2
0
        private void IterateCubes()
        {
            var grid = new List <D3Cube>();

            foreach (var neighbour in D3Cubes
                     .Where(c => c.IsActive)
                     .Select(cube => cube.GetNeighbours())
                     .SelectMany(neighbours => neighbours))
            {
                if (grid.Any(x => x.Location == neighbour))
                {
                    continue;
                }

                var current = D3Cubes.Any(c => c.Location == neighbour)
                    ? D3Cubes.FirstOrDefault(c => c.Location == neighbour)
                    : new D3Cube(neighbour);

                var activeNeighbours = D3Cubes
                                       .Count(c =>
                                              current.GetNeighbours().Contains(c.Location) &&
                                              c.Location != current.Location &&
                                              c.IsActive
                                              );

                if (activeNeighbours == 3 || (current.IsActive && activeNeighbours == 2))
                {
                    current.IsActive = true;
                }
                else
                {
                    current.IsActive = false;
                }

                grid.Add(current);
            }

            D3Cubes = grid;
        }