From c5732e107f3fc6c0ee2d49128955a69382f0b0be Mon Sep 17 00:00:00 2001 From: Sebastian Lindemeier Date: Thu, 3 Dec 2026 11:04:26 +0100 Subject: [PATCH] Refactor Day03 twisted square corner generation --- AoC_2026/Day03.cs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/AoC_2026/Day03.cs b/AoC_2026/Day03.cs index 9d03308..e1d217a 100644 --- a/AoC_2026/Day03.cs +++ b/AoC_2026/Day03.cs @@ -40,15 +40,15 @@ public class Day03 : IPuzzleSolver 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 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);