aoc2022/2.hs
2022-12-04 21:21:25 +01:00

21 lines
824 B
Haskell

{-# LANGUAGE ScopedTypeVariables #-}
import Data.Char
import Data.List.Split
import Data.List
main = do
ls <- readFile "2.in"
putStrLn . show . solveB . parse $ ls
where
solveA = sum . map evalGame
solveB = sum . map ( evalGame . forgeResult )
-- (=<<) id == flatten
parse = map ( charToOffset . (=<<) id) . map words . lines where
-- parse two chars as indices of (A) R/P/S moves or (B) loss/draw/win instructions
charToOffset (x:y:[]) = (ord x - ord 'A', ord y - ord 'X')
evalGame = sum . (<*>) [evalWin, evalMove] . pure where
evalWin (x, y) | x == y = 3
evalWin (x, y) | otherwise= (if ((x+1) `mod` 3 == y) then 6 else 0 )
evalMove (x, y) = y + 1
forgeResult (x, y) = ( x, (x + y - 1) `mod` 3 )