Refactor Day03 square counting to use corner delegates
This commit is contained in:
1 parent
ed104cf1a6
commit
20c08e9ce6
1 file changed
+19
-10
+19
-10
@@ -17,25 +17,27 @@ public class Day03 : IPuzzleSolver<long>
|
||||
public long SolvePart1(string pathToPuzzleInput)
|
||||
{
|
||||
var forestGrid = ParsePuzzleInput(pathToPuzzleInput);
|
||||
var numberOfSquares = GetNumberOfSquares(forestGrid);
|
||||
var numberOfSquares = GetNumberOfSquares(forestGrid, GetAllPossibleCornerCoordinates);
|
||||
return numberOfSquares;
|
||||
}
|
||||
|
||||
public long SolvePart2(string pathToPuzzleInput)
|
||||
{
|
||||
var forestGrid = ParsePuzzleInput(pathToPuzzleInput);
|
||||
var numberOfSquares = GetNumberOfSquares(forestGrid, true);
|
||||
var numberOfSquares = GetNumberOfSquares(forestGrid, GetAllPossibleSkewedCornerCoordinates);
|
||||
return numberOfSquares;
|
||||
}
|
||||
|
||||
private static int GetNumberOfSquares(char[][] forestGrid, bool twisted = false)
|
||||
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 => GetAllPossibleCornerCoordinates(topLeftCorner, gridWidth, gridLength, twisted)
|
||||
.Sum(topLeftCorner => getAllPossibleCorners(topLeftCorner, gridWidth, gridLength)
|
||||
.Count(coords => AllCoordinatesAreTrees(coords, forestGrid)));
|
||||
}
|
||||
|
||||
@@ -58,15 +60,22 @@ 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, bool canSkew)
|
||||
private static IEnumerable<(int x, int y)[]> GetAllPossibleCornerCoordinates((int x, int y) coord, int gridWidth, int gridLength)
|
||||
{
|
||||
var maxSquareWidth = canSkew
|
||||
? GetMaxSkewedSquareWidth(coord, gridWidth, gridLength)
|
||||
: GetMaxSquareWidth(coord, gridWidth, gridLength);
|
||||
var maxSquareWidth = 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;
|
||||
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);
|
||||
|
||||
Reference in new issue
Block a user