public
Created

Project Euler, problem 67 solution as coded at Czech Scala Enthusiasts' Scala soding dojo on Apr 9, 2013 Problem description: http://projecteuler.net/problem=67

  • Download Gist
Euler67.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
package com.czechscala.blank
 
import io.Source
 
object Euler67 extends App {
 
def solve(triangle: List[IndexedSeq[Int]]): Int = {
val lowestRow = triangle.foldLeft(IndexedSeq[Int]()) { (upper, lower) =>
val lowerRow = lower.zipWithIndex
lowerRow map { case (value, idx) =>
val left = upper lift (idx) getOrElse 0
val right = upper lift (idx - 1) getOrElse 0
value + math.max(left, right)
}
}
lowestRow.max
}
 
def parseLine(line: String): IndexedSeq[Int] = line split " " map (_.toInt)
 
def parseLines(lines: List[String]): List[IndexedSeq[Int]] = lines map parseLine
 
def readFile(fileName: String): List[String] = Source.fromFile(fileName).getLines().toList
 
println(solve(parseLines(readFile("triangle.txt"))))
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.