Skip to content

Instantly share code, notes, and snippets.

@vkostyukov
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vkostyukov/afe0e806e18335951b47 to your computer and use it in GitHub Desktop.
Save vkostyukov/afe0e806e18335951b47 to your computer and use it in GitHub Desktop.
CoconutDelivery
val energy = Console.readLine().toInt
val streams: List[(Int, Int, Int)] =
Iterator.continually(Console.readLine).takeWhile(_.nonEmpty).map { s: String =>
val stream = s.split(" ")
(stream(0).toInt, stream(1).toInt, stream(2).toInt)
} toList
val (_, end, _) = streams.maxBy {
case (_, to, _) => to
}
def cocount(i: Int, e: Int, s: List[(Int, Int, Int)]): Int =
if (i == end) e
else s.dropWhile {
case (from, _, _) => i > from
} match {
case (from, to, cost) :: tl =>
val spend = (from - i) * energy
cocount(to, e + spend + cost, tl) min
cocount(from, e + spend, tl)
case Nil => e + ((end - i) * energy)
}
println("Answer: " + cocount(0, 0, streams))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment