public virtual int GetDistanceOfShortestRoute(List<Track> railRoad, City origin, City destination) { var possibleRoutes = FindAllPossibleRoutes(railRoad, origin, destination); if (possibleRoutes.Count() == 0) throw new NoRouteException(); return possibleRoutes.OrderBy(x => x.DistanceFromStart).First(x => x.DistanceFromStart != 0).DistanceFromStart; }
protected List<RouteWithDistanceFromStart> FindAllPossibleRoutes(List<Track> railRoad, City origin, City finalDestination) { if (RailRoadHasNoTracks(railRoad)) throw new NoRouteException(); FinalDestination = finalDestination; possibleRoutes = new List<RouteWithDistanceFromStart>(); var initialRoute = StartRoute(origin); FindAllPossibleRoutes(railRoad, origin, initialRoute); return possibleRoutes; }
public virtual int GetNumberOfRoutesWithDistanceLimit(List<Track> railRoad, City origin, City destination, int distanceLimit) { this.distanceLimit = distanceLimit; var possibleRoutes = FindAllPossibleRoutes(railRoad, origin, destination); if (possibleRoutes.Count() == 0) throw new NoRouteException(); return possibleRoutes.Where(x => x.DistanceFromStart < distanceLimit).Count(); }
protected virtual int FindNumberOfPossibleRoutes(List<Track> railRoad, City origin, City finalDestination) { if (RailRoadHasNoTracks(railRoad)) throw new NoRouteException(); possibleRoutesCounter = 0; FinalDestination = finalDestination; var initialRoute = StartRoute(origin); FindNumberOfPossibleRoutes(railRoad, origin, initialRoute); return possibleRoutesCounter; }
public void ShouldIdentifyIfTheTrackConnectTheGivenCities() { var origin = new City("A"); var destination = new City("B"); var otherCity = new City("C"); var track = new Track(origin, destination, 4); track.IsAConnectionBetween(origin, destination).Should().BeTrue(); track.IsAConnectionBetween(origin, otherCity).Should().BeFalse(); }
public static Track Parse(string track) { try { var origin = new City(track[0].ToString()); var destination = new City(track[1].ToString()); var distance = Convert.ToInt32(track.Substring(2, track.Length - 2)); return new Track(origin, destination, distance); } catch (Exception e) { throw new FormatException("The input route is incorrect.", e); } }
private void FindAllPossibleRoutes(List<Track> railRoad, City origin, RouteWithDistanceFromStart runningRoute) { var possibleTracks = railRoad.FindAll(track => track.Origin.Equals(origin)); foreach (var track in possibleTracks) { var route = (RouteWithDistanceFromStart)runningRoute.Clone(); route.AppendCity(track.Destination, track.Distance); if (ThisTrackWillLeadMeToDestination(track)) possibleRoutes.Add(route); if (ShouldKeepRunningInThisRoute(runningRoute, track)) FindAllPossibleRoutes(railRoad, track.Destination, route); } }
private void FindNumberOfPossibleRoutes(List<Track> railRoad, City origin, Route runningRoute) { var possibleTracks = railRoad.FindAll(track => track.Origin.Equals(origin)); foreach (var track in possibleTracks) { var route = (Route)runningRoute.Clone(); route.AppendCity(track.Destination); if (ThisRouteMatchWithTheRequisites(route)) possibleRoutesCounter++; if (ShouldKeepRunningInThisRoute(route)) FindNumberOfPossibleRoutes(railRoad, track.Destination, route); } }
public void Should_Find_Number_Of_Routes_With_Limited_Distances_On_A_Simple_RailRoad() { var A = new City("A"); var B = new City("B"); var railRoad = new List<Track>(); railRoad.Add(new Track(A, B, 5)); railRoad.Add(new Track(A, B, 8)); railRoad.Add(new Track(A, B, 2)); railRoad.Add(new Track(A, B, 50)); railRoad.Add(new Track(A, B, 21)); railRoad.Add(new Track(A, B, 1)); railRoad.Add(new Track(A, B, 17)); railRoad.Add(new Track(A, B, 6)); routesFinder.GetNumberOfRoutesWithDistanceLimit(railRoad, A, B, 6).Should().Be(3); }
public Track(City origin, City destination, int distance) { Origin = origin; Destination = destination; Distance = distance; }
public bool IsAConnectionBetween(City origin, City destination) { return Origin.Equals(origin) && Destination.Equals(destination); }
private void EvaluateOriginAndDestination(Route route) { currentCity = route.Cities.Peek(); route.Cities.Dequeue(); nextCity = route.Cities.Peek(); }
public virtual int Find(List<Track> railroad, City origin, City finaldestination, int maxStops) { maximumStops = maxStops; return FindNumberOfPossibleRoutes(railroad, origin, finaldestination); }
private static RouteWithDistanceFromStart StartRoute(City origin) { var initialRoute = new RouteWithDistanceFromStart(); initialRoute.AppendCity(origin, 0); return initialRoute; }
private static Route StartRoute(City origin) { var initialRoute = new Route(); initialRoute.AppendCity(origin); return initialRoute; }
public virtual int FindPossibleRoutes(List<Track> railRoad, City origin, City finalDestionation, int exactNumberOfStops) { exactNumberOfStopsExpected = exactNumberOfStops; return FindNumberOfPossibleRoutes(railRoad, origin, finalDestionation); }
public void Should_Return_Zero_Routes_If_The_Origin_Is_A_Leaf() { var railRoad = GivenA.SimpleRailRoad(); var leaf = new City("Z"); routeFinder.FindPossibleRoutes(railRoad, leaf, new City("A"), 1).Should().Be(0); }
public void AppendCity(City city, int distance) { DistanceFromStart += distance; Cities.Enqueue(city); }