ProjectEular

-- Problem 22

module Main (main) where

import qualified List (sort)
import qualified Char (ord)

main :: IO ()
main = do
    names_str <- getContents
    print $ problem22 $ to_names names_str

problem22 :: [String] -> Int
problem22 names = sum $ zipWith (*) (map worth (List.sort names)) [1..(length names)]

worth :: String -> Int
worth name = sum $ map (\c -> Char.ord c - Char.ord 'A' + 1) name

to_names :: String -> [String]
to_names str = concat $ (map (map rm_quote . (wordsBy ',')) . lines) str

-- not good...
rm_quote :: String -> String
rm_quote = tail . init

-- see: implementation of words
wordsBy :: Char -> String -> [String]
wordsBy c s = case dropWhile (== c) s of
                "" -> []
                s' -> w : wordsBy c s''
                      where (w, s'') = break (== c) s'

トップ   差分 バックアップ リロード   一覧 単語検索 最終更新   ヘルプ   最終更新のRSS