Fichiers
advent-of-code-2022/src/Day2/Part2.hs
Jens Kadenbach 71972e30ec Day 2
2022-12-02 16:18:26 +01:00

43 lignes
1.0 KiB
Haskell

module Day2.Part2 (
day2_2,
readGameHints,
GameHint (GameHint),
Outcome (Win, Draw, Lose),
executePlan
) where
import Day2.Shared
data Outcome = Win | Draw | Lose deriving (Show, Eq)
newtype GameHint = GameHint (Shape, Outcome) deriving (Show, Eq)
readGameHints :: String -> [GameHint]
readGameHints = map parseGameHint . lines
parseGameHint :: String -> GameHint
parseGameHint [x, ' ', y] = GameHint (opponent x, me y)
where
opponent 'A' = Rock
opponent 'B' = Paper
opponent 'C' = Scissors
opponent _ = error $ "unknown shape: " ++ [x]
me 'X' = Lose
me 'Y' = Draw
me 'Z' = Win
me _ = error $ "unknown game result: " ++ [x]
parseGameHint xs = error $ "readGameHint not implemented for " ++ xs
playGames :: [GameHint] -> Score
playGames = foldMap (play . executePlan)
executePlan :: GameHint -> Game
executePlan (GameHint (x, Draw)) = Game (x, x)
executePlan (GameHint (x, Win)) = Game (x, winAgainst x)
executePlan (GameHint (x, Lose)) = Game (x, loseAgainst x)
day2_2 :: String -> Score
day2_2 = playGames . readGameHints