Last active
August 29, 2015 14:08
-
-
Save sungkmi/1b6655d57aebe80c6d87 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lascala | |
object TrainTimetable extends App { | |
def timetableToMin(timetable: List[String]): List[(Int, Int)] = { | |
class Time(s: String) { | |
val toMin = (s split ':' map (_.toInt) zip Seq(60, 1) map { case (x, y) => x * y }).sum | |
override val toString = "Time(" + toMin + ")" | |
} | |
val Array(s, e) = "09:00 12:00" split ' ' map (s => new Time(s)) | |
timetable.map { t => | |
val Array(s, e) = t split ' ' map (s => new Time(s)) | |
(s.toMin, e.toMin) | |
} | |
} | |
trait Event { val t: Int } | |
case class Arrival(t: Int) extends Event | |
case class Departure(t: Int) extends Event | |
def toEvents(timetable: List[String]): (List[Departure], List[Arrival]) = { | |
class Time(s: String) { | |
val toMin = (s split ':' map (_.toInt) zip Seq(60, 1) map { case (x, y) => x * y }).sum | |
override val toString = "Time(" + toMin + ")" | |
} | |
val (d, a) = List("09:00 12:00", "10:00 13:00", "11:00 12:30").map { t => | |
val Array(s, e) = t split ' ' map (s => new Time(s)) | |
(s.toMin, e.toMin) | |
}.unzip | |
(d map (t => Departure(t)), a map (t => Arrival(t))) | |
} | |
def numOfTrains(t: Int, fromA: List[String], fromB: List[String]): (Int, Int) = { | |
def numOfTrains0(fromA: List[(Int, Int)], fromB: List[(Int, Int)], currentA: Int, currentB: Int): (Int, Int) = { | |
(0, 1) | |
} | |
numOfTrains0(timetableToMin(fromA), timetableToMin(fromB), 0, 0) | |
} | |
def process(lineIn: Iterator[String])(lineOut: String => Unit) = | |
for (i <- 1 to lineIn.next().toInt) { | |
def read() = Seq.fill(lineIn.next().toInt)(lineIn.next()) | |
// lineOut(s"Case #$i: ${minSwitch(read(), read())}") | |
} | |
val writer = new java.io.PrintWriter("b.small.out") | |
try { | |
process(io.Source.fromFile("B-small-practice.in").getLines)(writer.println) | |
} finally { | |
writer.flush(); writer.close() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment