Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Part2.hs 1.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. module Day2.Part2 (
  2. day2_2,
  3. readGameHints,
  4. GameHint (GameHint),
  5. Outcome (Win, Draw, Lose),
  6. executePlan
  7. ) where
  8. import Day2.Shared
  9. data Outcome = Win | Draw | Lose deriving (Show, Eq)
  10. newtype GameHint = GameHint (Shape, Outcome) deriving (Show, Eq)
  11. readGameHints :: String -> [GameHint]
  12. readGameHints = map parseGameHint . lines
  13. parseGameHint :: String -> GameHint
  14. parseGameHint [x, ' ', y] = GameHint (opponent x, me y)
  15. where
  16. opponent 'A' = Rock
  17. opponent 'B' = Paper
  18. opponent 'C' = Scissors
  19. opponent _ = error $ "unknown shape: " ++ [x]
  20. me 'X' = Lose
  21. me 'Y' = Draw
  22. me 'Z' = Win
  23. me _ = error $ "unknown game result: " ++ [x]
  24. parseGameHint xs = error $ "readGameHint not implemented for " ++ xs
  25. playGames :: [GameHint] -> Score
  26. playGames = foldMap (play . executePlan)
  27. executePlan :: GameHint -> Game
  28. executePlan (GameHint (x, Draw)) = Game (x, x)
  29. executePlan (GameHint (x, Win)) = Game (x, winAgainst x)
  30. executePlan (GameHint (x, Lose)) = Game (x, loseAgainst x)
  31. day2_2 :: String -> Score
  32. day2_2 = playGames . readGameHints