Skip to content

Instantly share code, notes, and snippets.

@lutzh
Last active August 29, 2015 14:02
Show Gist options
  • Save lutzh/c1e9e23a7c5e7a61b787 to your computer and use it in GitHub Desktop.
Save lutzh/c1e9e23a7c5e7a61b787 to your computer and use it in GitHub Desktop.
def connections(from: Station, to: Station, departureTime: Time): Set[Seq[Hop]] = {
require(from != to)
def connections(from: Station, departureTime: Time, visitedStations: Set[Station]): Set[Seq[Hop]] =
{
val hopsFrom: Set[Hop] = hops.getOrElse(from, Set[Hop]())
hopsFrom.filter(hop => hop.departureTime >= departureTime)
.filterNot(hop => visitedStations.contains(hop.to))
.flatMap(hop =>
if (hop.to == to) Set(Seq[Hop](hop))
else
connections(hop.to, hop.arrivalTime, visitedStations + hop.from).map(seq => hop +: seq))
}
connections(from, departureTime, Set(from))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment