Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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
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"))))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment