{-# LANGUAGE ScopedTypeVariables #-} import Data.Char import Data.List.Split import Data.List import Data.Maybe main = do ls <- readFile "3.in" putStrLn . show . solveB $ ls where solveA = sum . map (priority . (!!0) . findPair) . map splitCompartments . lines solveB = sum . map (priority . (!!0) . findTriplets) . splitGroups . words splitGroups (a:b:c:xs) = (a,b,c):(splitGroups xs) splitGroups _ = [] splitCompartments x = (take half $ x, take half . drop half $ x ) where half = length x `div` 2 findPair :: (String, String) -> [Char] findPair (xs, ys) = maybeToList $ find (\x -> x `elem` ys) xs findTriplets :: (String,String,String) -> [Char] findTriplets (xs, ys, zs) = filterPair(xs, filterPair(ys, zs)) where filterPair (xs, ys) = filter (\x -> x `elem` ys) xs priority c | 'a' <= c && c <= 'z' = ord c - ord 'a' + 1 priority c | 'A' <= c && c <= 'Z' = ord c - ord 'A' + 27