Skip to content

Instantly share code, notes, and snippets.

@Riduidel
Created April 27, 2010 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Riduidel/380496 to your computer and use it in GitHub Desktop.
Save Riduidel/380496 to your computer and use it in GitHub Desktop.
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