using AdvenOfCode.Contracts; using AdventOfCode.Extensions; namespace AoC_2026; public class Day03 : IPuzzleSolver { private static char[][] ParsePuzzleInput(string path) { var puzzleInput = File.ReadAllLines(path) .Where(l => !string.IsNullOrWhiteSpace(l)) .Select(l => l.ToArray()) .ToArray(); return puzzleInput; } public long SolvePart1(string pathToPuzzleInput) { var forestGrid = ParsePuzzleInput(pathToPuzzleInput); var numberOfSquares = GetNumberOfSquares(forestGrid); return numberOfSquares; } private static int GetNumberOfSquares(char[][] forestGrid) { return forestGrid .Select((gridRow, i) => GetTreesInRow(gridRow) .Combinations(2) .Select(pair => (topCorners: pair, bottomIndex: GetBottomEdgeIndex(i, pair))) .Where(tuple => IsBottomEdgeInBounds(tuple.bottomIndex, forestGrid.Length)) .Count(tuple => IsValidSquare(forestGrid[tuple.bottomIndex], tuple.topCorners))) .Sum(); } public long SolvePart2(string pathToPuzzleInput) { var forestGrid = ParsePuzzleInput(pathToPuzzleInput); var numberOfSquares = GetNumberOfTwistedSquares(forestGrid); return numberOfSquares; } private static int GetNumberOfTwistedSquares(char[][] forestGrid) { 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) .Count(coords => AllCoordinatesAreTrees(coords, forestGrid))); } private static IEnumerable<(int x, int y)> GridToCoordinates(int width, int height) { return Enumerable .Range(0, height) .SelectMany(i => Enumerable .Range(0, width) .Select(j => (x: i, y: j))); } private static bool AllCoordinatesAreTrees((int x, int y)[] corners, char[][] forestGrid) { return corners.All(c => IsTreeAtCoordinate(c, forestGrid)); } private static bool IsTreeAtCoordinate((int x, int y) point, char[][] forestGrid) { return forestGrid[point.x][point.y] == '#'; } private static IEnumerable<(int x, int y)[]> GetAllPossibleCornerCoordinates((int x, int y) coord, int gridWidth, int gridLength) { 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 int GetMaxSquareWidth((int x, int y) coord, int gridWidth, int gridLength) { return gridLength < gridWidth ? gridLength - 1 - coord.x : gridWidth - 1 - Math.Max(0, coord.x - Math.Abs(gridLength - gridWidth)); } 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) { var corners = new (int x, int y)[4]; corners[0] = coord; corners[1] = (coord.x + skewAmount, coord.y + (squareWidth - skewAmount)); corners[2] = (coord.x + skewAmount + (squareWidth - skewAmount), coord.y + (squareWidth - skewAmount) - skewAmount); corners[3] = (coord.x + (squareWidth - skewAmount), coord.y - skewAmount); return corners; } private static int GetBottomEdgeIndex(int startRow, int[] pair) { return startRow + pair[1] - pair[0]; } private static bool IsBottomEdgeInBounds(int index, int forestGridLength) { return index < forestGridLength; } private static int[] GetTreesInRow(char[] row) { return [.. row.Index().Where(elem => elem.Item == '#').Select(elem => elem.Index)]; } private static bool IsValidSquare(char[] forestGridRow, int[] cornerPair) { return forestGridRow[cornerPair[0]] == '#' && forestGridRow[cornerPair[1]] == '#'; } }