32 lines
729 B
Haskell
32 lines
729 B
Haskell
import Data.Char (ord, isLower, isUpper)
|
|
import Data.List (intersect)
|
|
import Data.List.Split (chunksOf)
|
|
|
|
priority :: Char -> Int
|
|
priority c
|
|
| isLower c = ord c - ord 'a' + 1
|
|
| isUpper c = ord c - ord 'A' + 27
|
|
|
|
splitHalf :: [a] -> ([a],[a])
|
|
splitHalf xs = splitAt (length xs `div` 2) xs
|
|
|
|
findDup :: String -> Char
|
|
findDup s = head $ intersect l r where
|
|
(l, r) = splitHalf s
|
|
|
|
groupDup :: [String] -> Char
|
|
groupDup = head . foldr1 intersect
|
|
|
|
main :: IO ()
|
|
main = do
|
|
x <- lines <$> getContents
|
|
let dups = map findDup x
|
|
|
|
-- part 1
|
|
putStr "part 1: "
|
|
print $ sum (map priority dups)
|
|
|
|
-- part 2
|
|
let groups = chunksOf 3 x
|
|
putStr "part 2: "
|
|
print $ sum (map (priority . groupDup) groups) |