Files
advent-of-code-2024/10/HoofIt/Position.cs
T

35 lines
799 B
C#
Raw Normal View History

2024-12-10 20:52:40 -06:00
namespace HoofIt;
record Position(int X, int Y, int Height)
{
public bool TryGetNextPosition(Direction direction, string[] grid, out Position nextPosition)
{
2024-12-16 14:28:09 -06:00
var x = X + direction.Xd;
var y = Y + direction.Yd;
2024-12-10 20:52:40 -06:00
nextPosition = new(x, y, -1);
var isOffGrid = IsOffGrid(nextPosition, grid);
if (isOffGrid)
{
return false;
}
if (int.TryParse(grid[y][x].ToString(), out var nextPositionHeight) is false)
{
return false;
}
nextPosition = nextPosition with { Height = nextPositionHeight };
return true;
}
private static bool IsOffGrid(Position position, string[] grid)
{
return position.X < 0 ||
position.X > grid[0].Length - 1 ||
position.Y > grid.Length - 1 ||
position.Y < 0;
}
}