Exemplo n.º 1
0
        private static InfiniteGrid3D RunCycle(InfiniteGrid3D grid, bool useFourthDimension)
        {
            var gridClone = new InfiniteGrid3D(grid);

            var minBounds = grid.GetMinBounds();

            minBounds.X--;
            minBounds.Y--;
            minBounds.Z--;

            var maxBounds = grid.GetMaxBounds();

            maxBounds.X++;
            maxBounds.Y++;
            maxBounds.Z++;

            if (useFourthDimension)
            {
                minBounds.W--;
                maxBounds.W++;
            }

            for (int w = (int)minBounds.W; w <= (int)maxBounds.W; w++)
            {
                for (int z = (int)minBounds.Z; z <= (int)maxBounds.Z; z++)
                {
                    for (int y = (int)minBounds.Y; y <= (int)maxBounds.Y; y++)
                    {
                        for (int x = (int)minBounds.X; x <= (int)maxBounds.X; x++)
                        {
                            int numberOfActiveNeighbours = GetNumberOfActiveNeighbours(grid, x, y, z, w, useFourthDimension);

                            var cubeState = grid.GetPoint(x, y, z, w);

                            if (cubeState == CubeState.Active && (numberOfActiveNeighbours < 2 || numberOfActiveNeighbours > 3))
                            {
                                gridClone.SetPoint(x, y, z, w, CubeState.Inactive);
                            }
                            else if (cubeState == CubeState.Inactive && numberOfActiveNeighbours == 3)
                            {
                                gridClone.SetPoint(x, y, z, w, CubeState.Active);
                            }
                        }
                    }

                    PrintGrid(gridClone, z, w);
                }
            }

            return(gridClone);
        }
Exemplo n.º 2
0
        public static void StartA()
        {
            //var lines = File.ReadAllLines("Content\\Day17_Test.txt");
            var lines = File.ReadAllLines("Content\\Day17.txt");

            InfiniteGrid3D grid = new InfiniteGrid3D();

            for (var y = 0; y < lines.Length; y++)
            {
                var line = lines[y];

                for (var x = 0; x < line.Length; x++)
                {
                    char c = line[x];

                    grid.SetPoint(x, y, 0, 0, c == '#' ? CubeState.Active : CubeState.Inactive);
                }
            }

            PrintGrid(grid, 0, 0);

            for (int i = 0; i < 6; i++)
            {
                grid = RunCycle(grid, false);
            }

            int numberOfActiveCubes = grid.GetTotalPoints(CubeState.Active);

            Logger.Info($"Day 17A: {numberOfActiveCubes}");
        }