Created
March 18, 2014 10:21
-
-
Save trautonen/9617334 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
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