49 lines
901 B
Haskell
49 lines
901 B
Haskell
|
import System.IO
|
||
|
|
||
|
import Data.Char (ord)
|
||
|
|
||
|
import Data.Set (Set)
|
||
|
import qualified Data.Set as Set
|
||
|
|
||
|
import Data.List.Split
|
||
|
|
||
|
|
||
|
|
||
|
itemprio :: Char -> Int
|
||
|
itemprio c
|
||
|
| 'a' <= c && c <= 'z' = ord c - ord 'a' + 1
|
||
|
| 'A' <= c && c <= 'Z' = ord c - ord 'A' + 27
|
||
|
|
||
|
|
||
|
|
||
|
duplitems :: Ord a => [a] -> [a] -> [a]
|
||
|
duplitems c1 c2 = Set.elems $ Set.intersection s1 s2
|
||
|
where s1 = (Set.fromList c1)
|
||
|
s2 = (Set.fromList c2)
|
||
|
|
||
|
|
||
|
|
||
|
splitEqual :: Int -> [a] -> [[a]]
|
||
|
splitEqual n x = chunksOf (length x `div` n) x
|
||
|
|
||
|
|
||
|
|
||
|
rsprio :: String -> Int
|
||
|
rsprio x = itemprio $ head $ duplitems (head rs) (last rs)
|
||
|
where rs = splitEqual 2 x
|
||
|
|
||
|
|
||
|
|
||
|
grpprio :: [String] -> Int
|
||
|
grpprio = itemprio . head . foldr1 duplitems
|
||
|
|
||
|
|
||
|
|
||
|
handler :: String -> String
|
||
|
handler s = (show $ sum $ map rsprio $ lines s) ++ "\n" ++
|
||
|
(show $ sum $ map grpprio $ chunksOf 3 $ lines s) ++ "\n"
|
||
|
|
||
|
main :: IO ()
|
||
|
main = do
|
||
|
interact handler
|