Created
February 8, 2013 12:07
-
-
Save zerosum/4738562 to your computer and use it in GitHub Desktop.
Project Euler. Problem 22
This file contains hidden or 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
| 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