Files
Advent-Of-Code/AoC_2026/Day03.cs
T

131 lines
4.7 KiB
C#

using AdvenOfCode.Contracts;
using AdventOfCode.Extensions;
namespace AoC_2026;
public class Day03 : IPuzzleSolver<long>
{
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 coordinates = GridToCoordinates(forestGrid);
return coordinates
.Where(coord => IsTreeAtCoordinate(coord, forestGrid))
.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)));
}
private static IEnumerable<(int x, int y)> GridToCoordinates(char[][] forestGrid)
{
return Enumerable
.Range(0, forestGrid.Length)
.SelectMany(i => Enumerable
.Range(0, forestGrid[i].Length)
.Select(j => (x: i, y: j)));
}
private static bool AtLeastOneCoordinateIsToTheRight((int x, int y)[] corners, int currentRight)
{
return corners.Any(c => c.y > currentRight);
}
private static bool AllCoordinatesAreSameHeightOrBelow((int x, int y)[] corners, int currentHeight)
{
return corners.All(c => c.x >= currentHeight);
}
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 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)[]> GetAllPossibleCornerCoordinates(int gridWidth, (int x, int y) coord)
{
for (var squareWidth = gridWidth - 1; squareWidth > 0; squareWidth--)
{
for (var skewAmount = 0; skewAmount < gridWidth - 1; skewAmount++)
{
var corners = GetSquareCorners(coord, skewAmount, squareWidth);
yield return corners;
}
}
}
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]] == '#';
}
}