Refactor Day03 for readability

This commit is contained in:
Sebastian Lindemeier committed 2026-12-03 09:57:31 +01:00
1 parent d637b1f0fa
commit c8c96f57bc
1 file changed
+15 -15
+15 -15
View File
@@ -42,14 +42,14 @@ public class Day03 : IPuzzleSolver<long>
private int GetNumberOfTwistedSquares(char[][] forestGrid) private int GetNumberOfTwistedSquares(char[][] forestGrid)
{ {
var coords = GridToCoordinates(forestGrid); var coordinates = GridToCoordinates(forestGrid);
return coords return coordinates
.Where(coord => IsTreeAtPoint(coord, forestGrid)) .Where(coord => IsTreeAtCoordinate(coord, forestGrid))
.Sum(coord => GetAllPossibleCorners(forestGrid, coord.x, coord.y) .Sum(coord => GetAllPossibleCornerCoordinates(forestGrid, coord.x, coord.y)
.Where(c => AllPointsAreInBounds(c, forestGrid.Length, forestGrid[coord.x].Length)) .Where(coords => AllCoordinatesAreInBounds(coords, forestGrid.Length, forestGrid[coord.x].Length))
.Where(c => AllPointsAreSameHeightOrBelow(c, coord.x)) .Where(coords => AllCoordinatesAreSameHeightOrBelow(coords, coord.x))
.Where(c => OnePointIsToTheRight(c, coord.y)) .Where(coords => AtLeastOneCoordinateIsToTheRight(coords, coord.y))
.Count(c => AllPointsAreTrees(forestGrid, c))); .Count(coords => AllCoordinatesAreTrees(coords, forestGrid)));
} }
private static IEnumerable<(int x, int y)> GridToCoordinates(char[][] forestGrid) private static IEnumerable<(int x, int y)> GridToCoordinates(char[][] forestGrid)
@@ -61,32 +61,32 @@ public class Day03 : IPuzzleSolver<long>
.Select(j => (x: i, y: j))); .Select(j => (x: i, y: j)));
} }
private static bool OnePointIsToTheRight((int x, int y)[] corners, int currentRight) private static bool AtLeastOneCoordinateIsToTheRight((int x, int y)[] corners, int currentRight)
{ {
return corners.Any(c => c.y > currentRight); return corners.Any(c => c.y > currentRight);
} }
private static bool AllPointsAreSameHeightOrBelow((int x, int y)[] corners, int currentHeight) private static bool AllCoordinatesAreSameHeightOrBelow((int x, int y)[] corners, int currentHeight)
{ {
return corners.All(c => c.x >= currentHeight); return corners.All(c => c.x >= currentHeight);
} }
private static bool AllPointsAreTrees(char[][] forestGrid, (int x, int y)[] corners) private static bool AllCoordinatesAreTrees((int x, int y)[] corners, char[][] forestGrid)
{ {
return corners.All(c => IsTreeAtPoint(c, forestGrid)); return corners.All(c => IsTreeAtCoordinate(c, forestGrid));
} }
private static bool IsTreeAtPoint((int x, int y) point, char[][] forestGrid) private static bool IsTreeAtCoordinate((int x, int y) point, char[][] forestGrid)
{ {
return forestGrid[point.x][point.y] == '#'; return forestGrid[point.x][point.y] == '#';
} }
private static bool AllPointsAreInBounds((int x, int y)[] points, int length, int width) private static bool AllCoordinatesAreInBounds((int x, int y)[] points, int length, int width)
{ {
return points.All(p => p.x >= 0 && p.x < length && p.y >= 0 && p.y < width); return points.All(p => p.x >= 0 && p.x < length && p.y >= 0 && p.y < width);
} }
private static IEnumerable<(int x, int y)[]> GetAllPossibleCorners(char[][] forestGrid, int x, int y) private static IEnumerable<(int x, int y)[]> GetAllPossibleCornerCoordinates(char[][] forestGrid, int x, int y)
{ {
var maxSquareWidth = forestGrid[x].Length - 1; var maxSquareWidth = forestGrid[x].Length - 1;
for (var squareWidth = maxSquareWidth; squareWidth > 0; squareWidth--) for (var squareWidth = maxSquareWidth; squareWidth > 0; squareWidth--)