Skip to content

Instantly share code, notes, and snippets.

@lutzh
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save lutzh/c1e9e23a7c5e7a61b787 to your computer and use it in GitHub Desktop.

Select an option

Save lutzh/c1e9e23a7c5e7a61b787 to your computer and use it in GitHub Desktop.

Revisions

  1. lutzh revised this gist Jun 5, 2014. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,17 @@
    def connections(from: Station, to: Station, departureTime: Time): Set[Seq[Hop]] = {

    def connections(from: Station, to: Station, departureTime: Time): Set[Seq[Hop]] = {
    require(from != to)

    def connections(from: Station, to: Station, departureTime: Time, visitedStations: Set[Station]): Set[Seq[Hop]] =
    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, to, hop.arrivalTime, visitedStations + hop.from).map(seq => hop +: seq))
    connections(hop.to, hop.arrivalTime, visitedStations + hop.from).map(seq => hop +: seq))
    }
    connections(from, to, departureTime, Set(from))
    connections(from, departureTime, Set(from))
    }
    }
  2. lutzh created this gist Jun 5, 2014.
    15 changes: 15 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    def connections(from: Station, to: Station, departureTime: Time): Set[Seq[Hop]] = {
    require(from != to)

    def connections(from: Station, to: 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, to, hop.arrivalTime, visitedStations + hop.from).map(seq => hop +: seq))
    }
    connections(from, to, departureTime, Set(from))
    }