Browse Source

Cleanup day 1

main
Jens Kadenbach 1 year ago
parent
commit
2f60058c6c
3 changed files with 11 additions and 14 deletions
  1. 3
    3
      src/Day1.hs
  2. 7
    10
      src/Day1/Internal.hs
  3. 1
    1
      test/Spec.hs

+ 3
- 3
src/Day1.hs View File

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

+ 7
- 10
src/Day1/Internal.hs View File

1
 {-# LANGUAGE OverloadedStrings #-}
1
 {-# LANGUAGE OverloadedStrings #-}
2
 module Day1.Internal (
2
 module Day1.Internal (
3
-  elfToCalories,
3
+  parseElfInput,
4
   Elf,
4
   Elf,
5
   CalorieCount,
5
   CalorieCount,
6
   calories,
6
   calories,
8
   parseElfGroups,
8
   parseElfGroups,
9
   intOrZero,
9
   intOrZero,
10
   sortElves,
10
   sortElves,
11
-  caloriesOf
11
+  getCalories
12
 ) where
12
 ) where
13
 
13
 
14
 import qualified Data.Text as T
14
 import qualified Data.Text as T
25
 
25
 
26
 instance Show Elf where
26
 instance Show Elf where
27
   show (Elf (CalorieCount c)) = "Elf[" ++ show c ++ "]"
27
   show (Elf (CalorieCount c)) = "Elf[" ++ show c ++ "]"
28
-  
28
+
29
 instance Eq CalorieCount where
29
 instance Eq CalorieCount where
30
   (==) (CalorieCount c1) (CalorieCount c2)  = c1 == c2
30
   (==) (CalorieCount c1) (CalorieCount c2)  = c1 == c2
31
 
31
 
41
 calories :: Int -> CalorieCount
41
 calories :: Int -> CalorieCount
42
 calories = CalorieCount
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
 sortElves :: [Elf] -> [Elf]
47
 sortElves :: [Elf] -> [Elf]
48
 sortElves = L.reverse . L.sort
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
 parseElfGroups :: T.Text -> [[Int]]
53
 parseElfGroups :: T.Text -> [[Int]]
57
 parseElfGroups input = map (map intOrZero) groups
54
 parseElfGroups input = map (map intOrZero) groups

+ 1
- 1
test/Spec.hs View File

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

Loading…
Cancel
Save