project_euler/22.rb
Code:
# Project Euler Problem 22: Names Scores
# https://projecteuler.net/problem=22
require_relative './helpers.rb'
# Begin by sorting it into alphabetical order.
# Then working out the alphabetical value for each name,
# multiply this value by its alphabetical position in the list to
# obtain a name score.
def score(str)
str.chars.map { |c| c.ord - 64 }.sum
end
def solve(names)
names.sort.map.with_index { |name, i| score(name) * (i + 1) }.sum
end
puts solve(get_dataset(problem: 22)[0].gsub('"', '').split(','))
Output:
871198282