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)
|
public long SolvePart1(string pathToPuzzleInput)
|
||||||
{
|
{
|
||||||
var forestGrid = ParsePuzzleInput(pathToPuzzleInput);
|
var forestGrid = ParsePuzzleInput(pathToPuzzleInput);
|
||||||
var numberOfSquares = GetNumberOfSquares(forestGrid);
|
var numberOfSquares = GetNumberOfSquares(forestGrid, GetAllPossibleCornerCoordinates);
|
||||||
return numberOfSquares;
|
return numberOfSquares;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long SolvePart2(string pathToPuzzleInput)
|
public long SolvePart2(string pathToPuzzleInput)
|
||||||
{
|
{
|
||||||
var forestGrid = ParsePuzzleInput(pathToPuzzleInput);
|
var forestGrid = ParsePuzzleInput(pathToPuzzleInput);
|
||||||
var numberOfSquares = GetNumberOfSquares(forestGrid, true);
|
var numberOfSquares = GetNumberOfSquares(forestGrid, GetAllPossibleSkewedCornerCoordinates);
|
||||||
return numberOfSquares;
|
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 gridLength = forestGrid.Length;
|
||||||
var gridWidth = forestGrid[0].Length;
|
var gridWidth = forestGrid[0].Length;
|
||||||
var coordinates = GridToCoordinates(gridWidth - 1, gridLength - 1);
|
var coordinates = GridToCoordinates(gridWidth - 1, gridLength - 1);
|
||||||
return coordinates
|
return coordinates
|
||||||
.Where(coord => IsTreeAtCoordinate(coord, forestGrid))
|
.Where(coord => IsTreeAtCoordinate(coord, forestGrid))
|
||||||
.Sum(topLeftCorner => GetAllPossibleCornerCoordinates(topLeftCorner, gridWidth, gridLength, twisted)
|
.Sum(topLeftCorner => getAllPossibleCorners(topLeftCorner, gridWidth, gridLength)
|
||||||
.Count(coords => AllCoordinatesAreTrees(coords, forestGrid)));
|
.Count(coords => AllCoordinatesAreTrees(coords, forestGrid)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,15 +60,22 @@ public class Day03 : IPuzzleSolver<long>
|
|||||||
return forestGrid[point.x][point.y] == '#';
|
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
|
var maxSquareWidth = GetMaxSquareWidth(coord, gridWidth, gridLength);
|
||||||
? GetMaxSkewedSquareWidth(coord, gridWidth, gridLength)
|
|
||||||
: GetMaxSquareWidth(coord, gridWidth, gridLength);
|
|
||||||
for (var squareWidth = 1; squareWidth <= maxSquareWidth; squareWidth++)
|
for (var squareWidth = 1; squareWidth <= maxSquareWidth; squareWidth++)
|
||||||
{
|
{
|
||||||
var minSkew = canSkew ? GetMinSkew(coord, squareWidth, gridWidth) : 0;
|
yield return GetSquareCorners(coord, 0, squareWidth);
|
||||||
var maxSkew = canSkew ? GetMaxSkew(coord, squareWidth) : 0;
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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++)
|
for (var skewAmount = minSkew; skewAmount <= maxSkew; skewAmount++)
|
||||||
{
|
{
|
||||||
yield return GetSquareCorners(coord, skewAmount, squareWidth);
|
yield return GetSquareCorners(coord, skewAmount, squareWidth);
|
||||||
|
|||||||
Reference in new issue
Block a user