AoC/2022/day03/day03.hs

19 lines
493 B
Haskell
Raw Normal View History

2022-12-03 16:51:46 +01:00
import Data.List
import Data.List.Split
import Control.Arrow
2022-12-03 19:05:28 +01:00
import Control.Lens
2022-12-03 16:51:46 +01:00
main :: IO ()
main = do
input <- lines <$> readFile "input"
2022-12-03 19:05:28 +01:00
print $ both %~ sum $ (day3a &&& day3b) input
2022-12-03 16:51:46 +01:00
day3a :: [String] -> [Int]
2022-12-03 19:05:28 +01:00
day3a = map $ convert . uncurry intersect . (both %~ nub) . (flip splitAt <*> flip div 2 . length)
2022-12-03 16:51:46 +01:00
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