Skip to content

Commit

Permalink
2018 Day11 Solved
Browse files Browse the repository at this point in the history
  • Loading branch information
smabuk committed Nov 11, 2023
1 parent f48f1c6 commit 5bf484b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
33 changes: 32 additions & 1 deletion Solutions/2018/Day11.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 10 additions & 7 deletions Tests/2018/Tests_11.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}

0 comments on commit 5bf484b

Please sign in to comment.