Skip to content

Instantly share code, notes, and snippets.

@zerosum
Created February 8, 2013 12:07
Show Gist options
  • Select an option

  • Save zerosum/4738562 to your computer and use it in GitHub Desktop.

Select an option

Save zerosum/4738562 to your computer and use it in GitHub Desktop.
Project Euler. Problem 22
package euler.zerosum
import io.Source
object Euler0022 {
def main(args: Array[String]) {
val f = Source.fromFile("names.txt") // via. http://projecteuler.net/project/names.txt
println(namesScore(f.mkString.split(',').toList.map(_.filterNot(_ == '"')).sortWith(_<_)))
f.close()
}
private def namesScore(names: List[String]): Long = {
names.zipWithIndex.map(x => nameValue(x._1.toList) * (x._2 + 1)).sum
}
private def nameValue(name: List[Char]): Long = {
name.map(_.toUpper.hashCode() - 64L).sum
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment