Problem22
5000個以上の名前が書かれている46Kのテキストファイルfilenames.txt を用いる.
names.txt
http://odz.sakura.ne.jp/projecteuler/index.php?plugin=attach&refer=Problem%2022&openfile=names.txt
まずアルファベット順にソートせよ.のち, 各名前についてアルファベットに値を割り振り, リスト中の出現順の数と掛け合わせることで, 名前のスコアを計算する.
たとえば, リストがアルファベット順にソートされているとすると, COLINはリストの938番目にある. またCOLINは3 + 15 + 12 + 9 + 14 = 53という値を持つ. よってCOLINは938 × 53 = 49714というスコアを持つ.
ファイル中の全名前のスコアの合計を求めよ.
haskell
import List import Char main= readFile "names.txt" >>= print .sum . map (\t->fst t * snd t) . zip [1..] . map (sum . map ( (+(-64)).ord) ) . sort . words . translate translate []=[] translate (c:cs) |c==',' = ' ':cs_ |c=='"' = cs_ |otherwise= c:cs_ where cs_=translate cs
answer
871198282