Esempio n. 1
0
        public override object SolvePart1(int input)
        {
            var fuelGrid = new FuelGrid(input);

            var powerLevelSquares =
                from x in Enumerable.Range(1, 298)
                from y in Enumerable.Range(1, 298)
                let powerLevel = (
                    from xOff in Enumerable.Range(0, 3)
                    from yOff in Enumerable.Range(0, 3)
                    let xPos = x + xOff
                               let yPos = y + yOff
                                          select fuelGrid[xPos, yPos]
                    ).Sum()
                                 select(x, y, powerLevel);

            var max = powerLevelSquares.Aggregate((x: 0, y: 0, powerLevel: int.MinValue),
                                                  (prev, next) => (next.powerLevel > prev.powerLevel) ? next : prev);

            return($"{max.x},{max.y}");
        }
Esempio n. 2
0
        public override object SolvePart2(int input)
        {
            var fuelGridArray = new FuelGrid(input).ToArray(301);

            (int x, int y, int size)max = default;
            var maxValue = int.MinValue;

            for (var x = 1; x <= 300; x++)
            {
                for (var y = 1; y <= 300; y++)
                {
                    var maxBoxSize = 300 - Math.Max(x, y);
                    for (var size = 0; size < maxBoxSize; size++)
                    {
                        var powerLevel = 0;
                        for (var xOff = 0; xOff <= size; xOff++)
                        {
                            for (var yOff = 0; yOff <= size; yOff++)
                            {
                                var xPos = x + xOff;
                                var yPos = y + yOff;
                                powerLevel += fuelGridArray[xPos][yPos];
                            }
                        }

                        if (powerLevel > maxValue)
                        {
                            max      = (x, y, size + 1);
                            maxValue = powerLevel;
                        }
                    }
                }
            }

            return($"{max.x},{max.y},{max.size}({maxValue})");
        }
Esempio n. 3
0
        public void Test(int x, int y, int serialNumber, int expectedPowerLevel)
        {
            var calculatedPowerLevel = new FuelGrid(serialNumber)[x, y];

            Assert.Equal(expectedPowerLevel, calculatedPowerLevel);
        }