diff --git a/src/Day2/Shared.hs b/src/Day2/Shared.hs index 4bf348f..f20b1cf 100644 --- a/src/Day2/Shared.hs +++ b/src/Day2/Shared.hs @@ -12,7 +12,7 @@ module Day2.Shared ( ) where data Shape = Rock | Paper | Scissors - deriving (Show, Eq) + deriving (Show, Eq, Enum) newtype Score = Score Int deriving (Show, Eq) @@ -36,9 +36,7 @@ newtype Game = Game (Shape, Shape) deriving (Show, Eq) shapeScore :: Shape -> Score -shapeScore Rock = Score 1 -shapeScore Paper = Score 2 -shapeScore Scissors = Score 3 +shapeScore = Score . (+1) . fromEnum winAgainst :: Shape -> Shape winAgainst Rock = Paper diff --git a/test/Day2Spec.hs b/test/Day2Spec.hs index 389cdee..e019f7c 100644 --- a/test/Day2Spec.hs +++ b/test/Day2Spec.hs @@ -5,13 +5,20 @@ import Day2.Shared import Day2.Part1 import Day2.Part2 -input :: String +input :: String input = "A Y\nB X\nC Z" spec :: Spec spec = describe "Day2" $ do describe "Day2 - Shared" $ do + it "should implement winAgainst and loseAgainst for shapes" $ do + winAgainst Rock `shouldBe` Paper + winAgainst Paper `shouldBe` Scissors + winAgainst Scissors `shouldBe` Rock + loseAgainst Rock `shouldBe` Scissors + loseAgainst Paper `shouldBe` Rock + loseAgainst Scissors `shouldBe` Paper it "should have scores for shapes" $ do shapeScore Rock `shouldBe` Score 1 shapeScore Paper `shouldBe` Score 2