{-# LANGUAGE LambdaCase #-} module Main where import Data.List as L import Data.Bifunctor import Data.Char import Data.Maybe import Data.List.Extra import Safe data InputOptions = SumPrios | SumGroups | Quit getInput :: IO InputOptions getInput = do putStrLn "Sum Prios or Quit? (s/g/q)" getLine >>= \case "s" -> return SumPrios "g" -> return SumGroups "q" -> return Quit _ -> putStrLn "not understood" >> getInput main :: IO () main = getInput >>= \case Quit -> putStrLn "bye!" SumPrios -> interact $ (<>"\n") . show . sum . fmap turnIntoPrios . lines SumGroups -> interact $ (<>"\n") . show . sum . fmap turnIntoGroup . chunksOf 3 . lines turnIntoPrios :: String -> Integer turnIntoPrios l = maximum . fmap toPrio . uncurry intersect . bimap sort sort . splitAt (length l `div` 2) $ l turnIntoGroup :: [String] -> Integer turnIntoGroup [a,b,c] = maximum . fmap toPrio $ intersect a (b `intersect` c) toPrio :: Char -> Integer toPrio x | isAsciiLower x = toInteger $ ord x - ord 'a' + 1 | isAsciiUpper x = toInteger $ ord x - ord 'A' + 27 | otherwise = error "malformed input"