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

117 lines
4.5 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, GetAllPossibleCornerCoordinates);
return numberOfSquares;
}
public long SolvePart2(string pathToPuzzleInput)
{
var forestGrid = ParsePuzzleInput(pathToPuzzleInput);
var numberOfSquares = GetNumberOfSquares(forestGrid, GetAllPossibleSkewedCornerCoordinates);
return numberOfSquares;
}
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 => getAllPossibleCorners(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++)
{
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);
}
}
}
private static int GetMaxSquareWidth((int x, int y) coord, int gridWidth, int gridLength)
{
return Math.Min(gridLength - coord.x - 1, gridWidth - coord.y - 1);
}
private static int GetMaxSkewedSquareWidth((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 GetMinSkew((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;
}
}