コード例 #1
0
ファイル: Program.cs プロジェクト: apollo323au/AoC2018
        private SquareOfCells GenerateSquares(int[,] cells, int size)
        {
            SquareOfCells bestSquare = null;

            for (var x = 1; x + size - 1 <= 300; x++)
            {
                for (var y = 1; y + size - 1 <= 300; y++)
                {
                    var totalLevel = 0;

                    for (var xCoord = x; xCoord < x + size; xCoord++)
                    {
                        for (var yCoord = y; yCoord < y + size; yCoord++)
                        {
                            totalLevel += cells[xCoord - 1, yCoord - 1];
                        }
                    }

                    if (bestSquare == null || bestSquare.Level < totalLevel)
                    {
                        bestSquare = new SquareOfCells(totalLevel, size, x, y);
                    }
                }
            }

            return(bestSquare);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: apollo323au/AoC2018
        public string Calc2()
        {
            var           cells      = GenerateCells();
            SquareOfCells bestSquare = null;

            var bag = new ConcurrentBag <SquareOfCells>();

            Parallel.For(1, 300, size => bag.Add(GenerateSquares(cells, size)));

            var allSquares = bag.ToList();

            bestSquare = allSquares.OrderByDescending(s => s.Level).FirstOrDefault();
            //for (var size = 1; size <= 300; size++)
            //{
            //    var newBest = GenerateSquares(cells, size);
            //    if (bestSquare == null || bestSquare.Level < newBest.Level)
            //        bestSquare = newBest;
            //}

            return($"{bestSquare.X},{bestSquare.Y},{bestSquare.Size}");
        }