瀏覽代碼

Cleanup Day1 types

main
Jens Kadenbach 1 年之前
父節點
當前提交
024edd3e9d
共有 4 個文件被更改,包括 34 次插入40 次删除
  1. 3
    0
      aoc2022.cabal
  2. 3
    15
      src/Day1/Internal.hs
  3. 26
    0
      test/Day1Spec.hs
  4. 2
    25
      test/Spec.hs

+ 3
- 0
aoc2022.cabal 查看文件

@@ -27,6 +27,7 @@ library
27 27
   exposed-modules:
28 28
       Day1
29 29
       Day1.Internal
30
+      Day2
30 31
       Lib
31 32
   other-modules:
32 33
       Paths_aoc2022
@@ -61,6 +62,8 @@ test-suite aoc2022-test
61 62
   type: exitcode-stdio-1.0
62 63
   main-is: Spec.hs
63 64
   other-modules:
65
+      Day1Spec
66
+      Day2Spec
64 67
       Paths_aoc2022
65 68
   hs-source-dirs:
66 69
       test

+ 3
- 15
src/Day1/Internal.hs 查看文件

@@ -18,22 +18,10 @@ import qualified Data.List as L
18 18
 import Data.Either
19 19
 
20 20
 newtype CalorieCount = CalorieCount Int
21
-newtype Elf = Elf CalorieCount
22
-
23
-instance Show CalorieCount where
24
-  show (CalorieCount c) = "Calories[" ++ show c ++ "]"
25
-
26
-instance Show Elf where
27
-  show (Elf (CalorieCount c)) = "Elf[" ++ show c ++ "]"
21
+  deriving (Show, Eq, Ord)
28 22
 
29
-instance Eq CalorieCount where
30
-  (==) (CalorieCount c1) (CalorieCount c2)  = c1 == c2
31
-
32
-instance Eq Elf where
33
-  (==) (Elf (CalorieCount c1)) (Elf (CalorieCount c2))  = c1 == c2
34
-
35
-instance Ord Elf where
36
-  compare (Elf (CalorieCount c1)) (Elf (CalorieCount c2)) = compare c1 c2
23
+newtype Elf = Elf CalorieCount
24
+  deriving (Show, Eq, Ord)
37 25
 
38 26
 elf :: Int -> Elf
39 27
 elf c = Elf (CalorieCount c)

+ 26
- 0
test/Day1Spec.hs 查看文件

@@ -0,0 +1,26 @@
1
+{-# LANGUAGE OverloadedStrings #-}
2
+module Day1Spec (spec) where
3
+import Test.Hspec
4
+import Day1
5
+import Day1.Internal
6
+import qualified Data.Text as T
7
+
8
+spec :: Spec
9
+spec =
10
+  describe "Day1" $ do
11
+    it "sorts elves" $ do
12
+       let input = [ elf 6000, elf 4000, elf 11000]
13
+       sortElves input `shouldBe` [ elf 11000, elf 6000, elf 4000]
14
+    it "converts a list of strings to a list of either int or caloriecount" $ do
15
+       input <- readFile "ressources/day01-input-test"
16
+       parseElfInput (T.pack input) `shouldBe`
17
+          [ elf 6000, elf 4000, elf 11000, elf 24000, elf 10000 ]
18
+    it "parser text to int" $ do
19
+        intOrZero "1000" `shouldBe` 1000
20
+    it "finds the elf with the highest calorie count" $ do
21
+       input <- readFile "ressources/day01-input-test"
22
+       findElfWithHighestCalorieCount (T.pack input) `shouldBe` elf 24000
23
+    it "sums the calorie counts" $ do
24
+       input <- readFile "ressources/day01-input-test"
25
+       findCaloriesOfTop3Elves (T.pack input) `shouldBe` calories 45000
26
+

+ 2
- 25
test/Spec.hs 查看文件

@@ -1,25 +1,2 @@
1
-{-# LANGUAGE OverloadedStrings #-}
2
-import Test.Hspec
3
-import Day1
4
-import Day1.Internal
5
-import qualified Data.Text as T
6
-
7
-main :: IO ()
8
-main = hspec $ do
9
-  describe "Day1" $ do
10
-    it "sorts elves" $ do
11
-       let input = [ elf 6000, elf 4000, elf 11000]
12
-       sortElves input `shouldBe` [ elf 11000, elf 6000, elf 4000]
13
-    it "converts a list of strings to a list of either int or caloriecount" $ do
14
-       input <- readFile "ressources/day01-input-test"
15
-       parseElfInput (T.pack input) `shouldBe`
16
-          [ elf 6000, elf 4000, elf 11000, elf 24000, elf 10000 ]
17
-    it "parser text to int" $ do
18
-        intOrZero "1000" `shouldBe` 1000
19
-    it "finds the elf with the highest calorie count" $ do
20
-       input <- readFile "ressources/day01-input-test"
21
-       findElfWithHighestCalorieCount (T.pack input) `shouldBe` elf 24000
22
-    it "sums the calorie counts" $ do
23
-       input <- readFile "ressources/day01-input-test"
24
-       findCaloriesOfTop3Elves (T.pack input) `shouldBe` calories 45000
25
-
1
+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}
2
+module Main () where

Loading…
取消
儲存