{-# LANGUAGE OverloadedStrings #-}
module Euler.Problems.Problem22
(
InputText
, parseNames
, unquote
, answer
) where
import Euler.Prelude
import Euler.Util.WordScore
import qualified Data.List as List
import qualified Data.Text as Text
type InputText = Text
type Name = Text
answer :: InputText -> Integer
answer text =
List.sum scores
where
names = sort (parseNames text)
scores = List.zipWith (*) [1..] (fmap (wordScore . Text.unpack) names)
parseNames :: InputText -> [Name]
parseNames =
fmap (unquote . Text.strip) . Text.splitOn ","
unquote :: Text -> Text
unquote =
Text.dropWhile isQ . Text.dropWhileEnd isQ
where
isQ = (== '"')