diff --git a/AoC_2026/Day03.cs b/AoC_2026/Day03.cs index 42bf2b4..b7e84b8 100644 --- a/AoC_2026/Day03.cs +++ b/AoC_2026/Day03.cs @@ -17,28 +17,30 @@ public class Day03 : IPuzzleSolver public long SolvePart1(string pathToPuzzleInput) { var forestGrid = ParsePuzzleInput(pathToPuzzleInput); - var numberOfSquares = GetNumberOfSquares(forestGrid); + var numberOfSquares = GetNumberOfSquares(forestGrid, GetAllPossibleCornerCoordinates); return numberOfSquares; } public long SolvePart2(string pathToPuzzleInput) { var forestGrid = ParsePuzzleInput(pathToPuzzleInput); - var numberOfSquares = GetNumberOfSquares(forestGrid, true); + var numberOfSquares = GetNumberOfSquares(forestGrid, GetAllPossibleSkewedCornerCoordinates); return numberOfSquares; } - private static int GetNumberOfSquares(char[][] forestGrid, bool twisted = false) + private delegate IEnumerable<(int x, int y)[]> GetAllPossibleCornersDelegate((int x, int y) coord, int gridWidth, int gridLength); + + private static int GetNumberOfSquares(char[][] forestGrid, GetAllPossibleCornersDelegate getAllPossibleCorners) { 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(topLeftCorner, gridWidth, gridLength, twisted) + .Sum(topLeftCorner => getAllPossibleCorners(topLeftCorner, gridWidth, gridLength) .Count(coords => AllCoordinatesAreTrees(coords, forestGrid))); } - + private static IEnumerable<(int x, int y)> GridToCoordinates(int width, int height) { return Enumerable @@ -57,16 +59,23 @@ public class Day03 : IPuzzleSolver { return forestGrid[point.x][point.y] == '#'; } - - private static IEnumerable<(int x, int y)[]> GetAllPossibleCornerCoordinates((int x, int y) coord, int gridWidth, int gridLength, bool canSkew) + + private static IEnumerable<(int x, int y)[]> GetAllPossibleCornerCoordinates((int x, int y) coord, int gridWidth, int gridLength) { - var maxSquareWidth = canSkew - ? GetMaxSkewedSquareWidth(coord, gridWidth, gridLength) - : GetMaxSquareWidth(coord, gridWidth, gridLength); + var maxSquareWidth = GetMaxSquareWidth(coord, gridWidth, gridLength); for (var squareWidth = 1; squareWidth <= maxSquareWidth; squareWidth++) { - var minSkew = canSkew ? GetMinSkew(coord, squareWidth, gridWidth) : 0; - var maxSkew = canSkew ? GetMaxSkew(coord, squareWidth) : 0; + yield return GetSquareCorners(coord, 0, squareWidth); + } + } + + private static IEnumerable<(int x, int y)[]> GetAllPossibleSkewedCornerCoordinates((int x, int y) coord, int gridWidth, int gridLength) + { + var maxSquareWidth = GetMaxSkewedSquareWidth(coord, gridWidth, gridLength); + for (var squareWidth = 1; squareWidth <= maxSquareWidth; squareWidth++) + { + var minSkew = GetMinSkew(coord, squareWidth, gridWidth); + var maxSkew = GetMaxSkew(coord, squareWidth); for (var skewAmount = minSkew; skewAmount <= maxSkew; skewAmount++) { yield return GetSquareCorners(coord, skewAmount, squareWidth);