Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

17 lines (14 sloc) 0.575 kb
# Problem 22:
# Sort the names in 022_data.txt into alphabetical order, calculate the
# alphabetical value of each name (sum of character positions in the alphabet),
# and then calculate the total sum of all name alphabetical values multiplied by
# the names' position in the name list
total = 0
names = File.read('022_data.txt').scan((/"([^"]+)"/)).map { |a| a.first }.sort
names.each_with_index do |name,index|
alph_value = name.unpack('U*').inject(0) { |sum,char| sum + (char-64) }
total += alph_value * (index+1)
end
puts total
# Answer: 871198282
# Time: 0m0.095s
Jump to Line
Something went wrong with that request. Please try again.