24 lines
463 B
Haskell
24 lines
463 B
Haskell
|
{-# LANGUAGE NumericUnderscores #-}
|
||
|
module Roman (solution) where
|
||
|
|
||
|
solution :: String -> Int
|
||
|
solution = sum . mergeParts . map fromRoman
|
||
|
|
||
|
mergeParts :: [Int] -> [Int]
|
||
|
mergeParts [] = []
|
||
|
mergeParts [n1] = [n1]
|
||
|
mergeParts (n1:n2:rest)
|
||
|
| n1 < n2 = n2 - n1 : mergeParts rest
|
||
|
| otherwise = n1 : mergeParts (n2:rest)
|
||
|
|
||
|
fromRoman :: Char -> Int
|
||
|
fromRoman c = case c of
|
||
|
'I' -> 1
|
||
|
'V' -> 5
|
||
|
'X' -> 10
|
||
|
'L' -> 50
|
||
|
'C' -> 100
|
||
|
'D' -> 500
|
||
|
'M' -> 1_000
|
||
|
_ -> 0
|