Skip to content

Instantly share code, notes, and snippets.

@trautonen
Created March 18, 2014 10:21
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 trautonen/9617334 to your computer and use it in GitHub Desktop.
Save trautonen/9617334 to your computer and use it in GitHub Desktop.
import scala.io.Source
object OutRunScala {
def solve(source: Source): Int = {
source
.getLines()
.drop(1)
.map(_.trim)
.filterNot(_.isEmpty)
.map(_.split("\\s+"))
.map(_.map(_.toInt))
.foldLeft(IndexedSeq[Int]())(fold)
.max
}
private def fold(acc: IndexedSeq[Int], row: Array[Int]): IndexedSeq[Int] = {
for {
i <- 0 until row.size
n <- Some(row(i))
l <- if (i == acc.size) Some(0) else Some(acc(i))
r <- if (i == 0) Some(0) else Some(acc(i - 1))
} yield n + math.max(l, r)
}
def main(args: Array[String]) {
val start = System.currentTimeMillis
val result = solve(Source fromFile args(0))
val duration = System.currentTimeMillis - start
println(s"# result: $result")
println(s"# execution duration: $duration ms")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment