diff --git a/Solutions/2018/Day11.cs b/Solutions/2018/Day11.cs index fc5d83f..5583913 100644 --- a/Solutions/2018/Day11.cs +++ b/Solutions/2018/Day11.cs @@ -44,7 +44,38 @@ private static string Solution1(string[] input) { private static string Solution2(string[] input) { - return "** Solution not written yet **"; + const int MAX_GRID_SIZE = 21; // This works for me and keeps the speed down + int gridSerialNo = input[0].AsInt(); + + int[,] fuelCells = new int[300, 300]; + foreach ((int cellX, int cellY) in fuelCells.Walk2dArray()) { + int value = CalculatePowerValue(gridSerialNo, cellX + 1, cellY + 1); + fuelCells[cellX, cellY] = value; + } + + int maxValue = int.MinValue; + string topLeft = ""; + for (int y = 0; y < 300; y++) { + for (int x = 0; x < 300; x++) { + for (int gridSize = 3; gridSize < MAX_GRID_SIZE; gridSize++) { + if (x + gridSize >= 300 || y + gridSize >= 300) { + break; + } + int value = 0; + for (int dy = 0; dy < gridSize; dy++) { + for (int dx = 0; dx < gridSize; dx++) { + value += fuelCells[x + dx, y + dy]; + } + } + if (value > maxValue) { + maxValue = value; + topLeft = $"{x + 1},{y + 1},{gridSize}"; + } + } + } + } + + return topLeft; } public static int CalculatePowerValue(int gridSerialNo, int x, int y) diff --git a/Tests/2018/Tests_11.cs b/Tests/2018/Tests_11.cs index 8dd5fd2..668fde1 100644 --- a/Tests/2018/Tests_11.cs +++ b/Tests/2018/Tests_11.cs @@ -27,11 +27,14 @@ public void FuelCellP0werLevel(int gridSerialNo, int x, int y, int expected) Assert.Equal(expected, actual); } - //[Theory] - //[InlineData(21, 61)] - //public void Part2(string input, int expected) - //{ - // _ = int.TryParse(SolutionRouter.SolveProblem(YEAR, DAY, PART2, input, 32), out int actual); - // Assert.Equal(expected, actual); - //} + [Theory] + [InlineData("18", 90, 269, 16)] + [InlineData("42", 232, 251, 12)] + public void Part2(string input, int expectedX, int expectedY, int expectedSize) + { + string actual = SolutionRouter.SolveProblem(YEAR, DAY, PART2, input); + string expected = $"{expectedX},{expectedY},{expectedSize}"; + Assert.Equal(expected, actual); + } + }