Browse Source

Day2 use enum for shapes

main
Jens Kadenbach 1 year ago
parent
commit
5383662a81
2 changed files with 10 additions and 5 deletions
  1. 2
    4
      src/Day2/Shared.hs
  2. 8
    1
      test/Day2Spec.hs

+ 2
- 4
src/Day2/Shared.hs View File

12
 ) where
12
 ) where
13
 
13
 
14
 data Shape = Rock | Paper | Scissors
14
 data Shape = Rock | Paper | Scissors
15
-  deriving (Show, Eq)
15
+  deriving (Show, Eq, Enum)
16
 
16
 
17
 newtype Score = Score Int
17
 newtype Score = Score Int
18
   deriving (Show, Eq)
18
   deriving (Show, Eq)
36
   deriving (Show, Eq)
36
   deriving (Show, Eq)
37
 
37
 
38
 shapeScore :: Shape -> Score
38
 shapeScore :: Shape -> Score
39
-shapeScore Rock = Score 1
40
-shapeScore Paper = Score 2
41
-shapeScore Scissors = Score 3
39
+shapeScore = Score . (+1) . fromEnum
42
 
40
 
43
 winAgainst :: Shape -> Shape
41
 winAgainst :: Shape -> Shape
44
 winAgainst Rock = Paper
42
 winAgainst Rock = Paper

+ 8
- 1
test/Day2Spec.hs View File

5
 import Day2.Part1
5
 import Day2.Part1
6
 import Day2.Part2
6
 import Day2.Part2
7
 
7
 
8
-input :: String 
8
+input :: String
9
 input = "A Y\nB X\nC Z"
9
 input = "A Y\nB X\nC Z"
10
 
10
 
11
 spec :: Spec
11
 spec :: Spec
12
 spec =
12
 spec =
13
   describe "Day2" $ do
13
   describe "Day2" $ do
14
     describe "Day2 - Shared" $ do
14
     describe "Day2 - Shared" $ do
15
+      it "should implement winAgainst and loseAgainst for shapes" $ do
16
+        winAgainst Rock `shouldBe` Paper
17
+        winAgainst Paper `shouldBe` Scissors
18
+        winAgainst Scissors `shouldBe` Rock
19
+        loseAgainst Rock `shouldBe` Scissors
20
+        loseAgainst Paper `shouldBe` Rock
21
+        loseAgainst Scissors `shouldBe` Paper
15
       it "should have scores for shapes" $ do
22
       it "should have scores for shapes" $ do
16
         shapeScore Rock `shouldBe` Score 1
23
         shapeScore Rock `shouldBe` Score 1
17
         shapeScore Paper `shouldBe` Score 2
24
         shapeScore Paper `shouldBe` Score 2

Loading…
Cancel
Save