108 lines
4.0 KiB
C#
108 lines
4.0 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;
|
|
}
|
|
|
|
public long SolvePart2(string pathToPuzzleInput)
|
|
{
|
|
var forestGrid = ParsePuzzleInput(pathToPuzzleInput);
|
|
var numberOfSquares = GetNumberOfSquares(forestGrid, true);
|
|
return numberOfSquares;
|
|
}
|
|
|
|
private static int GetNumberOfSquares(char[][] forestGrid, bool twisted = false)
|
|
{
|
|
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)
|
|
.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, bool canSkew)
|
|
{
|
|
var maxSquareWidth = canSkew
|
|
? GetMaxSkewedSquareWidth(coord, gridWidth, gridLength)
|
|
: 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;
|
|
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;
|
|
}
|
|
} |