AdventOfCode/AdventOfCode.Core/Shared/A-Star/AStarGrid.cs
Rob 3846b42b7e Massive code base change
partial completion of day 3
2023-12-03 19:09:26 +01:00

47 lines
1.4 KiB
C#

namespace AdventOfCode.Core.Shared.A_Star
{
public class AStarGrid : Grid<AStarNode>
{
const int TILE_MOVE_COST = 10;
public AStarGrid(AStarNode[] data) : base(data)
{
}
public int MinimalSteps(AStarNode start, AStarNode destination)
{
List<AStarNode> closedNodes = new();
List<AStarNode> openNodes = new List<AStarNode> { start };
do
{
AStarNode current = openNodes.OrderBy(n => n.FCost).First();
openNodes.Remove(current);
current.CloseNode();
closedNodes.Add(current);
if (current == destination)
{
//done
return 1;
}
var neighbors = GetNeighbors(current, false);
neighbors = neighbors.Where(n => current.CanMoveTo(n));
// calc costs
foreach(AStarNode neighbor in neighbors)
{
neighbor.ParentNode = current;
// since we only move straight the move cost is always the same
neighbor.GCost = current.GCost + TILE_MOVE_COST;
neighbor.HCost = neighbor.DistanceTo(destination) * TILE_MOVE_COST;
neighbor.OpenNode();
openNodes.Add(neighbor);
}
}
while (true);
}
}
}