module Days.Day03 where

import AOCUtil
import Data.Char
import GHC.Utils.Misc

runA :: IO ()
runA = interactF "data/day03.txt" (show . solve . lines)

runB :: IO ()
runB = interactF "data/day03.txt" (show . solveB . lines)

solve :: [String] -> Int
solve = sum . map (getPrio . findDup . splitBackpack)

solveB :: [String] -> Int
solveB = sum . map (getPrio . findBadge) . chunkList 3

splitBackpack :: String -> (String, String)
splitBackpack s = (take l s, drop l s)
  where
    l = length s `div` 2

findDup :: (String, String) -> Char
findDup (ls, ls2) = head . filter (`elem` ls) $ ls2

getPrio :: Char -> Int
getPrio c
  | isLower c = fromEnum c - fromEnum 'a' + 1
  | isUpper c = fromEnum c - fromEnum 'A' + 27
  | otherwise = undefined

findBadge :: [String] -> Char
findBadge [a, b, c] = head . filter (`elem` a) . filter (`elem` b) $ c
