# -*- coding: utf-8 -*- # http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2022 # names = File.read("names.txt").split(",").map{|v| v.gsub(/[\s\"]+/,'')}.sort puts names.each_index.reduce(0) {|sum, i| # C = 3 ということから # 1 + 文字 - Aのアスキーコード # が文字のスコアなのでそれから全体のスコアを計算する. sum + ((i + 1) * names[i].chars.map{|s| 1 + s.bytes.next - "A".bytes.next}.reduce(:+)) }