# Pastebin FRqgV2Qn module Day2Gqplox where data Shape = Rock | Paper | Scissors deriving (Show, Ord, Eq, Enum) data Outcome = Win | Draw | Lose deriving (Show, Ord, Eq, Enum) shapeScore :: Shape -> Int shapeScore = succ . fromEnum score :: Shape -> Outcome -> Int score opp Win = 6 + (shapeScore . wrappedSucc) opp score opp Draw = 3 + shapeScore opp score opp Lose = 0 + (shapeScore . wrappedPred) opp wrappedSucc :: Shape -> Shape wrappedSucc Scissors = Rock wrappedSucc s = succ s wrappedPred :: Shape -> Shape wrappedPred = wrappedSucc . wrappedSucc parseShape :: Char -> Either String Shape parseShape 'A' = Right Rock parseShape 'B' = Right Paper parseShape 'C' = Right Scissors parseShape x = Left ("can't parse shape: " <> show x) parseOutcome :: Char -> Either String Outcome parseOutcome 'X' = Right Lose parseOutcome 'Y' = Right Draw parseOutcome 'Z' = Right Win parseOutcome x = Left ("can't parse outcome " <> show x) solve :: [(Shape, Outcome)] -> Int solve = sum . map (uncurry score) parse :: String -> Either String (Shape, Outcome) parse str = case str of [s, ' ', o] -> (,) <$> parseShape s <*> parseOutcome o mi -> Left ("Malformed input line: " <> show mi) part2 :: IO (Either String Int) part2 = fmap solve . traverse parse . lines <$> readFile "input/day2"