132 lines
4.6 KiB
C#
132 lines
4.6 KiB
C#
using AdvenOfCode.Contracts;
|
|
using AdventOfCode.Extensions;
|
|
|
|
namespace AoC_2026;
|
|
|
|
public class Day03 : IPuzzleSolver<long>
|
|
{
|
|
private char[][] ParsePuzzleInput(string path)
|
|
{
|
|
var puzzleInput = File.ReadAllLines(path)
|
|
.Where(l => !string.IsNullOrWhiteSpace(l))
|
|
.Select(l => l.Select(c => c).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) =>
|
|
GetTopCorners(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 int GetNumberOfTwistedSquares(char[][] forestGrid)
|
|
{
|
|
var coords = GridToCoordinates(forestGrid);
|
|
return coords
|
|
.Where(coord => IsTreeAtPoint(coord, forestGrid))
|
|
.Sum(coord => GetAllPossibleCorners(forestGrid, coord.x, coord.y)
|
|
.Where(c => AllPointsAreInBounds(c, forestGrid.Length, forestGrid[coord.x].Length))
|
|
.Where(c => AllPointsAreSameHeightOrBelow(c, coord.x))
|
|
.Where(c => OnePointIsToTheRight(c, coord.y))
|
|
.Count(c => AllPointsAreTrees(forestGrid, c)));
|
|
}
|
|
|
|
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 OnePointIsToTheRight((int x, int y)[] corners, int currentRight)
|
|
{
|
|
return corners.Any(c => c.y > currentRight);
|
|
}
|
|
|
|
private static bool AllPointsAreSameHeightOrBelow((int x, int y)[] corners, int currentHeight)
|
|
{
|
|
return corners.All(c => c.x >= currentHeight);
|
|
}
|
|
|
|
private static bool AllPointsAreTrees(char[][] forestGrid, (int x, int y)[] corners)
|
|
{
|
|
return corners.All(c => IsTreeAtPoint(c, forestGrid));
|
|
}
|
|
|
|
private static bool IsTreeAtPoint((int x, int y) point, char[][] forestGrid)
|
|
{
|
|
return forestGrid[point.x][point.y] == '#';
|
|
}
|
|
|
|
private static bool AllPointsAreInBounds((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)[]> GetAllPossibleCorners(char[][] forestGrid, int x, int y)
|
|
{
|
|
var maxSquareWidth = forestGrid[x].Length - 1;
|
|
for (var squareWidth = maxSquareWidth; squareWidth > 0; squareWidth--)
|
|
{
|
|
for (var skewAmount = 0; skewAmount < maxSquareWidth; skewAmount++)
|
|
{
|
|
var corners = GetSkewedSquareCorners(x, y, skewAmount, squareWidth);
|
|
yield return corners;
|
|
}
|
|
}
|
|
}
|
|
|
|
private static (int x, int y)[] GetSkewedSquareCorners(int x, int y, int skewAmount, int squareWidth)
|
|
{
|
|
var corners = new (int x, int y)[4];
|
|
corners[0] = (x, y);
|
|
corners[1] = (x + skewAmount, y + (squareWidth - skewAmount));
|
|
corners[2] = (x + skewAmount + (squareWidth - skewAmount), y + (squareWidth - skewAmount) - skewAmount);
|
|
corners[3] = (x + (squareWidth - skewAmount), 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[] GetTopCorners(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]] == '#';
|
|
}
|
|
} |