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
+30 -27
+30 -27
View File
@@ -42,35 +42,24 @@ public class Day03 : IPuzzleSolver<long>
private static int GetNumberOfTwistedSquares(char[][] forestGrid) 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 return coordinates
.Where(coord => IsTreeAtCoordinate(coord, forestGrid)) .Where(coord => IsTreeAtCoordinate(coord, forestGrid))
.Sum(topLeftCorner => GetAllPossibleCornerCoordinates(forestGrid[topLeftCorner.x].Length, topLeftCorner) .Sum(topLeftCorner => GetAllPossibleCornerCoordinates(topLeftCorner, gridWidth, gridLength)
.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))); .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 return Enumerable
.Range(0, forestGrid.Length) .Range(0, height)
.SelectMany(i => Enumerable .SelectMany(i => Enumerable
.Range(0, forestGrid[i].Length) .Range(0, width)
.Select(j => (x: i, y: j))); .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) private static bool AllCoordinatesAreTrees((int x, int y)[] corners, char[][] forestGrid)
{ {
return corners.All(c => IsTreeAtCoordinate(c, forestGrid)); return corners.All(c => IsTreeAtCoordinate(c, forestGrid));
@@ -81,21 +70,35 @@ public class Day03 : IPuzzleSolver<long>
return forestGrid[point.x][point.y] == '#'; 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); var maxSquareWidth = GetMaxSquareWidth(coord, gridWidth, gridLength);
for (var squareWidth = 1; squareWidth <= maxSquareWidth; squareWidth++)
{
var minSkew = GetMinSkewAmount(coord, squareWidth, gridWidth);
var maxSkew = GetMaxSkew(coord, squareWidth);
for (var skewAmount = minSkew; skewAmount <= maxSkew; skewAmount++)
{
yield return GetSquareCorners(coord, skewAmount, squareWidth);
}
}
} }
private static IEnumerable<(int x, int y)[]> GetAllPossibleCornerCoordinates(int gridWidth, (int x, int y) coord) private static int GetMaxSquareWidth((int x, int y) coord, int gridWidth, int gridLength)
{ {
for (var squareWidth = gridWidth - 1; squareWidth > 0; squareWidth--) return gridLength < gridWidth
{ ? gridLength - 1 - coord.x
for (var skewAmount = 0; skewAmount < gridWidth - 1; skewAmount++) : gridWidth - 1 - Math.Max(0, coord.x - Math.Abs(gridLength - gridWidth));
{
var corners = GetSquareCorners(coord, skewAmount, squareWidth);
yield return corners;
} }
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) private static (int x, int y)[] GetSquareCorners((int x, int y) coord, int skewAmount, int squareWidth)