AoC/2022/day02/day02.hs

30 lines
848 B
Haskell
Raw Normal View History

2022-12-02 17:48:13 +01:00
data Hand = Rock | Paper | Scissor deriving (Eq,Enum,Bounded,Show)
instance Ord Hand where
compare x y = case fromEnum x - fromEnum y `mod` 3 of
0 -> EQ
1 -> GT
(-2) -> GT
_ -> LT
(+.) :: Hand -> Int -> Hand
(+.) a b = toEnum $ flip mod 3 $ (+) (fromEnum a) b
main :: IO ()
main = do
input <- map (concat . words) . lines <$> readFile "input"
print $ day2 id input
print $ day2 realStrat input
toHands :: [Char] -> [Hand]
toHands x = map toEnum $ (zipWith subtract [65,88]) (map fromEnum x)
realStrat :: [Hand] -> [Hand]
realStrat (a:b:[]) = [a,a +. fromEnum (b +. 2)]
day2 :: ([Hand] -> [Hand]) -> [[Char]] -> Int
day2 f = sum . map (play . f . toHands)
play :: [Hand] -> Int
play (x:y:[]) = fromEnum y + 1 + if x == y then 3 else fromEnum (x<y) * 6