aoc2022/3-b.hs

34 lines
859 B
Haskell

{-# LANGUAGE DataKinds #-}
import Data.Char
import Data.Maybe
getCharPriority c
| isLower c = fromEnum c - 96
| isUpper c = fromEnum c - 64 + 26
| otherwise = undefined
allChars :: [Char]
allChars = map toEnum $ [65..90] ++ [96..122]
-- this is STUPID
unwrapJust (Just a) = a
unwrapJust Nothing = undefined
findFirstJust :: [Maybe a] -> a
findFirstJust a = unwrapJust $ head $ filter isJust a
findDuplicate :: [String] -> Char
findDuplicate [l1, l2, l3] = findFirstJust $ map (\c ->
if c`elem`l1 && c`elem`l2 && c`elem`l3 then Just c else Nothing) allChars
findDuplicate _ = undefined
extractIntoGroups :: Int -> [String] -> [[String]]
extractIntoGroups s l
| s > length l = []
| otherwise = take s l : extractIntoGroups s (drop s l)
main = interact $
show . sum
. map (getCharPriority . findDuplicate)
. extractIntoGroups 3
. lines