Ejemplo n.º 1
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}");
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        private static int GetNumberOfActiveNeighbours(InfiniteGrid3D grid, int x, int y, int z, int w, bool useFourthDimension)
        {
            int activeNeighbours = 0;

            void InnerLoop(int newW)
            {
                for (int newZ = z - 1; newZ <= z + 1; newZ++)
                {
                    for (int newY = y - 1; newY <= y + 1; newY++)
                    {
                        for (int newX = x - 1; newX <= x + 1; newX++)
                        {
                            if (newX == x && newY == y && z == newZ && w == newW)
                            {
                                continue;
                            }

                            //Logger.Debug($"{newX} {newY} {newZ} {newW}");

                            if (grid.GetPoint(newX, newY, newZ, newW) == CubeState.Active)
                            {
                                activeNeighbours++;
                            }
                        }
                    }
                }
            }

            if (useFourthDimension)
            {
                for (int newW = w - 1; newW <= w + 1; newW++)
                {
                    InnerLoop(newW);
                }
            }
            else
            {
                InnerLoop(0);
            }

            return(activeNeighbours);
        }
Ejemplo n.º 4
0
        private static void PrintGrid(InfiniteGrid3D grid, int z, int w)
        {
            Logger.Debug($"z={z}, w={w}");

            var minBounds = grid.GetMinBounds();
            var maxBounds = grid.GetMaxBounds();

            for (int y = (int)minBounds.Y; y <= (int)maxBounds.Y; y++)
            {
                string line = string.Empty;

                for (int x = (int)minBounds.X; x <= (int)maxBounds.X; x++)
                {
                    line += grid.GetPoint(x, y, z, w) == CubeState.Active ? "#" : ".";
                }

                Logger.Debug(line);
            }

            Logger.Debug(string.Empty);
        }
Ejemplo n.º 5
0
 public InfiniteGrid3D(InfiniteGrid3D grid)
 {
     _points    = grid._points.ToDictionary(x => x.Key, x => x.Value);
     _minBounds = grid._minBounds;
     _maxBounds = grid._maxBounds;
 }