From c8c96f57bccff2ea178ab511f6f43ad0030df170 Mon Sep 17 00:00:00 2001 From: Sebastian Lindemeier Date: Thu, 3 Dec 2026 09:57:31 +0100 Subject: [PATCH] Refactor Day03 for readability --- AoC_2026/Day03.cs | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/AoC_2026/Day03.cs b/AoC_2026/Day03.cs index eea5201..18b5e11 100644 --- a/AoC_2026/Day03.cs +++ b/AoC_2026/Day03.cs @@ -42,14 +42,14 @@ public class Day03 : IPuzzleSolver private int GetNumberOfTwistedSquares(char[][] forestGrid) { - var coords = GridToCoordinates(forestGrid); - return coords - .Where(coord => IsTreeAtPoint(coord, forestGrid)) - .Sum(coord => GetAllPossibleCorners(forestGrid, coord.x, coord.y) - .Where(c => AllPointsAreInBounds(c, forestGrid.Length, forestGrid[coord.x].Length)) - .Where(c => AllPointsAreSameHeightOrBelow(c, coord.x)) - .Where(c => OnePointIsToTheRight(c, coord.y)) - .Count(c => AllPointsAreTrees(forestGrid, c))); + 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)) + .Count(coords => AllCoordinatesAreTrees(coords, forestGrid))); } private static IEnumerable<(int x, int y)> GridToCoordinates(char[][] forestGrid) @@ -61,32 +61,32 @@ public class Day03 : IPuzzleSolver .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); } - 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); } - 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] == '#'; } - 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); } - 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; for (var squareWidth = maxSquareWidth; squareWidth > 0; squareWidth--)