improve performance 10x by reducing redundant bound checks and optimizing corner generation

This commit is contained in:
Sebastian Lindemeier committed 2026-12-03 14:47:29 +01:00
1 parent 3dde3cbe22
commit 8efceb5a02
1 file changed
+31 -28
+31 -28
View File
@@ -42,35 +42,24 @@ public class Day03 : IPuzzleSolver<long>
private static int GetNumberOfTwistedSquares(char[][] forestGrid)
{
var coordinates = GridToCoordinates(forestGrid);
var gridLength = forestGrid.Length;
var gridWidth = forestGrid[0].Length;
var coordinates = GridToCoordinates(gridWidth - 1, gridLength - 1);
return coordinates
.Where(coord => IsTreeAtCoordinate(coord, forestGrid))
.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))
.Sum(topLeftCorner => GetAllPossibleCornerCoordinates(topLeftCorner, gridWidth, gridLength)
.Count(coords => AllCoordinatesAreTrees(coords, forestGrid)));
}
private static IEnumerable<(int x, int y)> GridToCoordinates(char[][] forestGrid)
private static IEnumerable<(int x, int y)> GridToCoordinates(int width, int height)
{
return Enumerable
.Range(0, forestGrid.Length)
.Range(0, height)
.SelectMany(i => Enumerable
.Range(0, forestGrid[i].Length)
.Range(0, width)
.Select(j => (x: i, y: j)));
}
private static bool AtLeastOneCoordinateIsToTheRight((int x, int y)[] corners, int currentRight)
{
return corners.Any(c => c.y > currentRight);
}
private static bool AllCoordinatesAreSameHeightOrBelow((int x, int y)[] corners, int currentHeight)
{
return corners.All(c => c.x >= currentHeight);
}
private static bool AllCoordinatesAreTrees((int x, int y)[] corners, char[][] forestGrid)
{
return corners.All(c => IsTreeAtCoordinate(c, forestGrid));
@@ -81,23 +70,37 @@ public class Day03 : IPuzzleSolver<long>
return forestGrid[point.x][point.y] == '#';
}
private static bool AllCoordinatesAreInBounds((int x, int y)[] points, int length, int width)
private static IEnumerable<(int x, int y)[]> GetAllPossibleCornerCoordinates((int x, int y) coord, int gridWidth, int gridLength)
{
return points.All(p => p.x >= 0 && p.x < length && p.y >= 0 && p.y < width);
}
private static IEnumerable<(int x, int y)[]> GetAllPossibleCornerCoordinates(int gridWidth, (int x, int y) coord)
{
for (var squareWidth = gridWidth - 1; squareWidth > 0; squareWidth--)
var maxSquareWidth = GetMaxSquareWidth(coord, gridWidth, gridLength);
for (var squareWidth = 1; squareWidth <= maxSquareWidth; squareWidth++)
{
for (var skewAmount = 0; skewAmount < gridWidth - 1; skewAmount++)
var minSkew = GetMinSkewAmount(coord, squareWidth, gridWidth);
var maxSkew = GetMaxSkew(coord, squareWidth);
for (var skewAmount = minSkew; skewAmount <= maxSkew; skewAmount++)
{
var corners = GetSquareCorners(coord, skewAmount, squareWidth);
yield return corners;
yield return GetSquareCorners(coord, skewAmount, squareWidth);
}
}
}
private static int GetMaxSquareWidth((int x, int y) coord, int gridWidth, int gridLength)
{
return gridLength < gridWidth
? gridLength - 1 - coord.x
: gridWidth - 1 - Math.Max(0, coord.x - Math.Abs(gridLength - gridWidth));
}
private static int GetMaxSkew((int x, int y) coord, int squareWidth)
{
return Math.Min(coord.y, squareWidth - 1);
}
private static int GetMinSkewAmount((int x, int y) coord, int squareWidth, int gridWidth)
{
return Math.Max(coord.y + squareWidth, gridWidth - 1) % (gridWidth - 1);
}
private static (int x, int y)[] GetSquareCorners((int x, int y) coord, int skewAmount, int squareWidth)
{
var corners = new (int x, int y)[4];