public static DirectionSpeed FromPathVariant(PathVariant path) { return(new DirectionSpeed() { Direction = path.Direction, Speed = path.Speed, }); }
private void Update(PathVariant path, Direction direction, int acceleration) { var newPath = path.AddMove(direction, acceleration); var newPoint = newPath.Point; if (newPoint.IsValid(newPath.Speed)) { var dict = currentVariants[newPoint]; var key = DirectionSpeed.FromPathVariant(newPath); if (!dict.TryGetValue(key, out var old) || newPath.IsBetterThan(old)) { dict[key] = newPath; hasChanges = true; } } }
public PathVariant AddMove(Direction direction, int acceleration) { var result = new PathVariant(); Moves.ForEach(result.Moves.Add); result.Speed = Speed + acceleration; result.Direction = direction; result.TotalTime += 1m / result.Speed; result.Point = Point.AddDirection(direction); result.Moves.Add(new Move() { Direction = direction, Acceleration = acceleration, Point = result.Point }); return(result); }
public (Direction, int) WhereToGo(Point currentLocation, Direction currentDirection, int currentVelocity) { currentVariants = Graph.Nodes.Keys .ToDictionary(p => p, p => new Dictionary <DirectionSpeed, PathVariant>()); var pathToCurrent = new PathVariant() { Point = currentLocation, Speed = currentVelocity, Direction = currentDirection, }; currentVariants[currentLocation] = new Dictionary <DirectionSpeed, PathVariant>() { { DirectionSpeed.FromPathVariant(pathToCurrent), pathToCurrent } }; do { hasChanges = false; foreach (var pathToSource in currentVariants.Values.SelectMany(d => d.Values).ToList()) { for (var acceleration = -30; acceleration <= 30; acceleration += 10) { foreach (Direction dir in Enum.GetValues(typeof(Direction))) { Update(pathToSource, dir, acceleration); } } } }while (hasChanges); var bestPath = currentVariants[Finish].Values.OrderBy(x => x.TotalTime).First(); var lastKnown = bestPath.Moves .TakeWhile(p => Graph.Nodes[p.Point] != HexType.Unknown) .Last().Point; var result = currentVariants[lastKnown].Values.OrderBy(x => x.TotalTime).First().Moves.First(); return(result.Direction, result.Acceleration); }
public bool IsBetterThan(PathVariant other) => TotalTime < other.TotalTime;