namespace AdventOfCode.Core.Shared.A_Star { public class AStarGrid : Grid { const int TILE_MOVE_COST = 10; public AStarGrid(AStarNode[] data) : base(data) { } public int MinimalSteps(AStarNode start, AStarNode destination) { List closedNodes = new(); List openNodes = new List { 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); } } }