Refactor Day03 square counting to share logic

This commit is contained in:
Sebastian Lindemeier committed 2026-12-03 16:19:43 +01:00
1 parent 8efceb5a02
commit ed104cf1a6
1 file changed
+17 -43
+17 -43
View File
@@ -21,33 +21,21 @@ public class Day03 : IPuzzleSolver<long>
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);
var numberOfSquares = GetNumberOfSquares(forestGrid, true);
return numberOfSquares;
}
private static int GetNumberOfTwistedSquares(char[][] forestGrid)
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)
.Sum(topLeftCorner => GetAllPossibleCornerCoordinates(topLeftCorner, gridWidth, gridLength, twisted)
.Count(coords => AllCoordinatesAreTrees(coords, forestGrid)));
}
@@ -70,13 +58,15 @@ public class Day03 : IPuzzleSolver<long>
return forestGrid[point.x][point.y] == '#';
}
private static IEnumerable<(int x, int y)[]> GetAllPossibleCornerCoordinates((int x, int y) coord, int gridWidth, int gridLength)
private static IEnumerable<(int x, int y)[]> GetAllPossibleCornerCoordinates((int x, int y) coord, int gridWidth, int gridLength, bool canSkew)
{
var maxSquareWidth = GetMaxSquareWidth(coord, gridWidth, gridLength);
var maxSquareWidth = canSkew
? GetMaxSkewedSquareWidth(coord, gridWidth, gridLength)
: GetMaxSquareWidth(coord, gridWidth, gridLength);
for (var squareWidth = 1; squareWidth <= maxSquareWidth; squareWidth++)
{
var minSkew = GetMinSkewAmount(coord, squareWidth, gridWidth);
var maxSkew = GetMaxSkew(coord, 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);
@@ -86,7 +76,12 @@ public class Day03 : IPuzzleSolver<long>
private static int GetMaxSquareWidth((int x, int y) coord, int gridWidth, int gridLength)
{
return gridLength < gridWidth
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));
}
@@ -96,7 +91,7 @@ public class Day03 : IPuzzleSolver<long>
return Math.Min(coord.y, squareWidth - 1);
}
private static int GetMinSkewAmount((int x, int y) coord, int squareWidth, int gridWidth)
private static int GetMinSkew((int x, int y) coord, int squareWidth, int gridWidth)
{
return Math.Max(coord.y + squareWidth, gridWidth - 1) % (gridWidth - 1);
}
@@ -110,25 +105,4 @@ public class Day03 : IPuzzleSolver<long>
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]] == '#';
}
}