Created
April 27, 2010 08:26
-
-
Save Riduidel/380496 to your computer and use it in GitHub Desktop.
This file contains 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
def process(line) { | |
def returned = new TreeSet() | |
line.split(",").each { elt -> | |
returned.add(elt) | |
} | |
return returned | |
} | |
def compute(name, position, mapping) { | |
def sum = new BigInteger(0); | |
name.each { elt -> | |
def character = elt as char | |
if(mapping.containsKey(character)) | |
sum += mapping.get(character) | |
} | |
return sum*position | |
} | |
def mapping = new TreeMap(); | |
for(i in 0..25) { | |
mapping[i.plus('A' as char) as char] = new BigInteger(i+1); | |
} | |
def wholeSum = new BigInteger(0); | |
def wholeCollection = new TreeSet(); | |
new File("names.txt").eachLine { | |
wholeCollection.addAll(process(it)) | |
} | |
wholeCollection.eachWithIndex {entry, index -> | |
// Damn, positions are 0-offset, while Euler expects a 1-offset ! | |
wholeSum += compute(entry, new BigInteger(index+1), mapping) | |
} | |
println wholeSum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment