public KeyValuePair <Point, int> GetHighestPowerSquare(int?squareSize) { var highestPower = 0; KeyValuePair <Point, int> target = new KeyValuePair <Point, int>(); var currentSize = squareSize.HasValue ? squareSize.Value : 1; while (currentSize <= Size.Width) { Console.WriteLine($"Calculating for {currentSize}x{currentSize}"); for (var i = 0; i + currentSize < Size.Width; i++) { for (var j = 0; j + currentSize < Size.Height; j++) { FuelCell fuelCell = Grid[i, j]; var totalPowerLevel = GetTotalPowerLevelForSquare(fuelCell, currentSize); if (totalPowerLevel != null && totalPowerLevel > highestPower) { target = new KeyValuePair <Point, int>(fuelCell.Coordinate, currentSize); highestPower = (int)totalPowerLevel; } } } // Break the loop if squareSize has a value. currentSize += squareSize.HasValue ? Size.Width : 1; } // Grid is 0-indexed but the question expects 1-indexed, so add 1 here where it's inconsequential. return(new KeyValuePair <Point, int>(new Point(target.Key.X + 1, target.Key.Y + 1), target.Value)); }
public FuelGrid(Size size, int gridSerialNumber) { Size = size; Grid = new FuelCell[size.Width, size.Height]; for (var i = 0; i < Size.Width; i++) { for (var j = 0; j < Size.Height; j++) { Grid[i, j] = new FuelCell(new Point(i, j), gridSerialNumber); } } }
public int?GetTotalPowerLevelForSquare(FuelCell cell, int squareSize) { int?totalPower = null; if (cell.Coordinate.X + squareSize - 1 < Size.Width && cell.Coordinate.Y + squareSize - 1 < Size.Height) { totalPower = 0; for (int i = 0; i < squareSize; i++) { for (int j = 0; j < squareSize; j++) { totalPower += Grid[cell.Coordinate.X + i, cell.Coordinate.Y + j].PowerLevel; } } } return(totalPower); }