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") } }