Refactor Day03 square counting to use corner delegates

This commit is contained in:
Sebastian Lindemeier committed 2026-12-03 16:39:04 +01:00
1 parent ed104cf1a6
commit 20c08e9ce6
1 file changed
+21 -12
+21 -12
View File
@@ -17,28 +17,30 @@ public class Day03 : IPuzzleSolver<long>
public long SolvePart1(string pathToPuzzleInput) public long SolvePart1(string pathToPuzzleInput)
{ {
var forestGrid = ParsePuzzleInput(pathToPuzzleInput); var forestGrid = ParsePuzzleInput(pathToPuzzleInput);
var numberOfSquares = GetNumberOfSquares(forestGrid); var numberOfSquares = GetNumberOfSquares(forestGrid, GetAllPossibleCornerCoordinates);
return numberOfSquares; return numberOfSquares;
} }
public long SolvePart2(string pathToPuzzleInput) public long SolvePart2(string pathToPuzzleInput)
{ {
var forestGrid = ParsePuzzleInput(pathToPuzzleInput); var forestGrid = ParsePuzzleInput(pathToPuzzleInput);
var numberOfSquares = GetNumberOfSquares(forestGrid, true); var numberOfSquares = GetNumberOfSquares(forestGrid, GetAllPossibleSkewedCornerCoordinates);
return numberOfSquares; return numberOfSquares;
} }
private static int GetNumberOfSquares(char[][] forestGrid, bool twisted = false) private delegate IEnumerable<(int x, int y)[]> GetAllPossibleCornersDelegate((int x, int y) coord, int gridWidth, int gridLength);
private static int GetNumberOfSquares(char[][] forestGrid, GetAllPossibleCornersDelegate getAllPossibleCorners)
{ {
var gridLength = forestGrid.Length; var gridLength = forestGrid.Length;
var gridWidth = forestGrid[0].Length; var gridWidth = forestGrid[0].Length;
var coordinates = GridToCoordinates(gridWidth - 1, gridLength - 1); var coordinates = GridToCoordinates(gridWidth - 1, gridLength - 1);
return coordinates return coordinates
.Where(coord => IsTreeAtCoordinate(coord, forestGrid)) .Where(coord => IsTreeAtCoordinate(coord, forestGrid))
.Sum(topLeftCorner => GetAllPossibleCornerCoordinates(topLeftCorner, gridWidth, gridLength, twisted) .Sum(topLeftCorner => getAllPossibleCorners(topLeftCorner, gridWidth, gridLength)
.Count(coords => AllCoordinatesAreTrees(coords, forestGrid))); .Count(coords => AllCoordinatesAreTrees(coords, forestGrid)));
} }
private static IEnumerable<(int x, int y)> GridToCoordinates(int width, int height) private static IEnumerable<(int x, int y)> GridToCoordinates(int width, int height)
{ {
return Enumerable return Enumerable
@@ -57,16 +59,23 @@ public class Day03 : IPuzzleSolver<long>
{ {
return forestGrid[point.x][point.y] == '#'; return forestGrid[point.x][point.y] == '#';
} }
private static IEnumerable<(int x, int y)[]> GetAllPossibleCornerCoordinates((int x, int y) coord, int gridWidth, int gridLength, bool canSkew) private static IEnumerable<(int x, int y)[]> GetAllPossibleCornerCoordinates((int x, int y) coord, int gridWidth, int gridLength)
{ {
var maxSquareWidth = canSkew var maxSquareWidth = GetMaxSquareWidth(coord, gridWidth, gridLength);
? GetMaxSkewedSquareWidth(coord, gridWidth, gridLength)
: GetMaxSquareWidth(coord, gridWidth, gridLength);
for (var squareWidth = 1; squareWidth <= maxSquareWidth; squareWidth++) for (var squareWidth = 1; squareWidth <= maxSquareWidth; squareWidth++)
{ {
var minSkew = canSkew ? GetMinSkew(coord, squareWidth, gridWidth) : 0; yield return GetSquareCorners(coord, 0, squareWidth);
var maxSkew = canSkew ? GetMaxSkew(coord, squareWidth) : 0; }
}
private static IEnumerable<(int x, int y)[]> GetAllPossibleSkewedCornerCoordinates((int x, int y) coord, int gridWidth, int gridLength)
{
var maxSquareWidth = GetMaxSkewedSquareWidth(coord, gridWidth, gridLength);
for (var squareWidth = 1; squareWidth <= maxSquareWidth; squareWidth++)
{
var minSkew = GetMinSkew(coord, squareWidth, gridWidth);
var maxSkew = GetMaxSkew(coord, squareWidth);
for (var skewAmount = minSkew; skewAmount <= maxSkew; skewAmount++) for (var skewAmount = minSkew; skewAmount <= maxSkew; skewAmount++)
{ {
yield return GetSquareCorners(coord, skewAmount, squareWidth); yield return GetSquareCorners(coord, skewAmount, squareWidth);