Refactor Day03 twisted square corner generation

This commit is contained in:
Sebastian Lindemeier committed 2026-12-03 11:04:26 +01:00
1 parent 6617f4a883
commit c5732e107f
1 file changed
+10 -11
+10 -11
View File
@@ -40,15 +40,15 @@ public class Day03 : IPuzzleSolver<long>
return numberOfSquares;
}
private int GetNumberOfTwistedSquares(char[][] forestGrid)
private static int GetNumberOfTwistedSquares(char[][] forestGrid)
{
var coordinates = GridToCoordinates(forestGrid);
return coordinates
.Where(coord => IsTreeAtCoordinate(coord, forestGrid))
.Sum(coord => GetAllPossibleCornerCoordinates(forestGrid, coord.x, coord.y)
.Where(coords => AllCoordinatesAreInBounds(coords, forestGrid.Length, forestGrid[coord.x].Length))
.Where(coords => AllCoordinatesAreSameHeightOrBelow(coords, coord.x))
.Where(coords => AtLeastOneCoordinateIsToTheRight(coords, coord.y))
.Sum(topLeftCorner => GetAllPossibleCornerCoordinates(forestGrid[topLeftCorner.x].Length, topLeftCorner)
.Where(coords => AllCoordinatesAreInBounds(coords, forestGrid.Length, forestGrid[topLeftCorner.x].Length))
.Where(coords => AllCoordinatesAreSameHeightOrBelow(coords, topLeftCorner.x))
.Where(coords => AtLeastOneCoordinateIsToTheRight(coords, topLeftCorner.y))
.Count(coords => AllCoordinatesAreTrees(coords, forestGrid)));
}
@@ -86,20 +86,19 @@ public class Day03 : IPuzzleSolver<long>
return points.All(p => p.x >= 0 && p.x < length && p.y >= 0 && p.y < width);
}
private static IEnumerable<(int x, int y)[]> GetAllPossibleCornerCoordinates(char[][] forestGrid, int x, int y)
private static IEnumerable<(int x, int y)[]> GetAllPossibleCornerCoordinates(int gridWidth, (int x, int y) coord)
{
var maxSquareWidth = forestGrid[x].Length - 1;
for (var squareWidth = maxSquareWidth; squareWidth > 0; squareWidth--)
for (var squareWidth = gridWidth - 1; squareWidth > 0; squareWidth--)
{
for (var skewAmount = 0; skewAmount < maxSquareWidth; skewAmount++)
for (var skewAmount = 0; skewAmount < gridWidth - 1; skewAmount++)
{
var corners = GetSkewedSquareCorners(x, y, skewAmount, squareWidth);
var corners = GetSquareCorners(coord.x, coord.y, skewAmount, squareWidth);
yield return corners;
}
}
}
private static (int x, int y)[] GetSkewedSquareCorners(int x, int y, int skewAmount, int squareWidth)
private static (int x, int y)[] GetSquareCorners(int x, int y, int skewAmount, int squareWidth)
{
var corners = new (int x, int y)[4];
corners[0] = (x, y);