瀏覽代碼

Cleanup day 1

main
Jens Kadenbach 1 年之前
父節點
當前提交
2f60058c6c
共有 3 個文件被更改,包括 11 次插入14 次删除
  1. 3
    3
      src/Day1.hs
  2. 7
    10
      src/Day1/Internal.hs
  3. 1
    1
      test/Spec.hs

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

@@ -30,10 +30,10 @@ day1_2 = do
30 30
 
31 31
 findCaloriesOfTop3Elves :: T.Text -> CalorieCount
32 32
 findCaloriesOfTop3Elves  =
33
-  calories . sumCalories . take 3 . sortElves . elfToCalories
33
+  calories . sumCalories . take 3 . sortElves . parseElfInput
34 34
   where
35 35
     sumCalories :: [Elf] -> Int
36
-    sumCalories = sum . map caloriesOf
36
+    sumCalories = sum . map getCalories
37 37
 
38 38
 findElfWithHighestCalorieCount :: T.Text -> Elf
39
-findElfWithHighestCalorieCount = head .sortElves . elfToCalories
39
+findElfWithHighestCalorieCount = head . sortElves . parseElfInput

+ 7
- 10
src/Day1/Internal.hs 查看文件

@@ -1,6 +1,6 @@
1 1
 {-# LANGUAGE OverloadedStrings #-}
2 2
 module Day1.Internal (
3
-  elfToCalories,
3
+  parseElfInput,
4 4
   Elf,
5 5
   CalorieCount,
6 6
   calories,
@@ -8,7 +8,7 @@ module Day1.Internal (
8 8
   parseElfGroups,
9 9
   intOrZero,
10 10
   sortElves,
11
-  caloriesOf
11
+  getCalories
12 12
 ) where
13 13
 
14 14
 import qualified Data.Text as T
@@ -25,7 +25,7 @@ instance Show CalorieCount where
25 25
 
26 26
 instance Show Elf where
27 27
   show (Elf (CalorieCount c)) = "Elf[" ++ show c ++ "]"
28
-  
28
+
29 29
 instance Eq CalorieCount where
30 30
   (==) (CalorieCount c1) (CalorieCount c2)  = c1 == c2
31 31
 
@@ -41,17 +41,14 @@ elf c = Elf (CalorieCount c)
41 41
 calories :: Int -> CalorieCount
42 42
 calories = CalorieCount
43 43
 
44
-caloriesOf :: Elf -> Int
45
-caloriesOf (Elf (CalorieCount c)) = c
44
+getCalories :: Elf -> Int
45
+getCalories (Elf (CalorieCount c)) = c
46 46
 
47 47
 sortElves :: [Elf] -> [Elf]
48 48
 sortElves = L.reverse . L.sort
49 49
 
50
-elfToCalories :: T.Text -> [Elf]
51
-elfToCalories input = map elf sums
52
-  where
53
-    groups = parseElfGroups input
54
-    sums = map sum groups
50
+parseElfInput :: T.Text -> [Elf]
51
+parseElfInput input = map (elf . sum) (parseElfGroups input)
55 52
 
56 53
 parseElfGroups :: T.Text -> [[Int]]
57 54
 parseElfGroups input = map (map intOrZero) groups

+ 1
- 1
test/Spec.hs 查看文件

@@ -12,7 +12,7 @@ main = hspec $ do
12 12
        sortElves input `shouldBe` [ elf 11000, elf 6000, elf 4000]
13 13
     it "converts a list of strings to a list of either int or caloriecount" $ do
14 14
        input <- readFile "ressources/day01-input-test"
15
-       elfToCalories (T.pack input) `shouldBe`
15
+       parseElfInput (T.pack input) `shouldBe`
16 16
           [ elf 6000, elf 4000, elf 11000, elf 24000, elf 10000 ]
17 17
     it "parser text to int" $ do
18 18
         intOrZero "1000" `shouldBe` 1000

Loading…
取消
儲存