示例#1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Day 17 - Conway Cubes");
            Console.WriteLine("Star 1");
            Console.WriteLine();

            char[,] grid = File.ReadAllLines(inputFile).ToArrayGrid();

            {
                HashSet <Point3D> activeStates     = new HashSet <Point3D>();
                HashSet <Point3D> tempActiveStates = new HashSet <Point3D>();

                for (int x = 0; x < grid.GetLength(0); x++)
                {
                    for (int y = 0; y < grid.GetLength(1); y++)
                    {
                        if (grid[x, y] == '#')
                        {
                            activeStates.Add((x, y, 0));
                        }
                    }
                }

                int cycleCount = 0;


                while (cycleCount < 6)
                {
                    //Perform a cycle
                    tempActiveStates.Clear();

                    Point3D min = new Point3D(int.MaxValue, int.MaxValue, int.MaxValue);
                    Point3D max = new Point3D(int.MinValue, int.MinValue, int.MinValue);

                    foreach (Point3D point in activeStates)
                    {
                        min = AoCMath.Min(min, point);
                        max = AoCMath.Max(max, point);
                    }

                    for (int x = min.x - 1; x <= max.x + 1; x++)
                    {
                        for (int y = min.y - 1; y <= max.y + 1; y++)
                        {
                            for (int z = min.z - 1; z <= max.z + 1; z++)
                            {
                                if (CheckState(activeStates, x, y, z))
                                {
                                    tempActiveStates.Add(new Point3D(x, y, z));
                                }
                            }
                        }
                    }

                    cycleCount++;
                    (activeStates, tempActiveStates) = (tempActiveStates, activeStates);
                }


                int output1 = activeStates.Count();

                Console.WriteLine($"The answer is: {output1}");
            }

            Console.WriteLine();
            Console.WriteLine("Star 2");
            Console.WriteLine();

            {
                HashSet <Point4D> activeStates     = new HashSet <Point4D>();
                HashSet <Point4D> tempActiveStates = new HashSet <Point4D>();

                for (int x = 0; x < grid.GetLength(0); x++)
                {
                    for (int y = 0; y < grid.GetLength(1); y++)
                    {
                        if (grid[x, y] == '#')
                        {
                            activeStates.Add((x, y, 0, 0));
                        }
                    }
                }

                int cycleCount = 0;


                while (cycleCount < 6)
                {
                    //Perform a cycle
                    tempActiveStates.Clear();

                    Point4D min = new Point4D(int.MaxValue, int.MaxValue, int.MaxValue, int.MaxValue);
                    Point4D max = new Point4D(int.MinValue, int.MinValue, int.MinValue, int.MinValue);;

                    foreach (Point4D point in activeStates)
                    {
                        min = AoCMath.Min(min, point);
                        max = AoCMath.Max(max, point);
                    }

                    for (int x = min.x - 1; x <= max.x + 1; x++)
                    {
                        for (int y = min.y - 1; y <= max.y + 1; y++)
                        {
                            for (int z = min.z - 1; z <= max.z + 1; z++)
                            {
                                for (int w = min.w - 1; w <= max.w + 1; w++)
                                {
                                    if (CheckState(activeStates, x, y, z, w))
                                    {
                                        tempActiveStates.Add(new Point4D(x, y, z, w));
                                    }
                                }
                            }
                        }
                    }

                    cycleCount++;
                    (activeStates, tempActiveStates) = (tempActiveStates, activeStates);
                }


                int output2 = activeStates.Count();

                Console.WriteLine($"The answer is: {output2}");
            }

            Console.WriteLine();
            Console.ReadKey();
        }