import Control.Arrow
import Control.Lens
import Data.List
import Data.List.Split

main :: IO ()
main = do
  input <- lines <$> readFile "input"
  print $ both %~ sum $ (day3a &&& day3b) input

day3a :: [String] -> [Int]
day3a =
  map $
  convert .
  uncurry intersect . (both %~ nub) . (splitAt =<< flip div 2 . length)

day3b :: [String] -> [Int]
day3b = map (convert . foldl1 intersect) . chunksOf 3

convert :: String -> Int
convert =
  (\x ->
     if x > 90
       then x - 96
       else x - 38) .
  fromEnum . head
