Cleanup Day1 types

This commit is contained in:
Jens Kadenbach
2022-12-02 11:53:44 +01:00
parent 2f60058c6c
commit 024edd3e9d
4 changed files with 34 additions and 40 deletions

View File

@@ -27,6 +27,7 @@ library
exposed-modules: exposed-modules:
Day1 Day1
Day1.Internal Day1.Internal
Day2
Lib Lib
other-modules: other-modules:
Paths_aoc2022 Paths_aoc2022
@@ -61,6 +62,8 @@ test-suite aoc2022-test
type: exitcode-stdio-1.0 type: exitcode-stdio-1.0
main-is: Spec.hs main-is: Spec.hs
other-modules: other-modules:
Day1Spec
Day2Spec
Paths_aoc2022 Paths_aoc2022
hs-source-dirs: hs-source-dirs:
test test

View File

@@ -18,22 +18,10 @@ import qualified Data.List as L
import Data.Either import Data.Either
newtype CalorieCount = CalorieCount Int newtype CalorieCount = CalorieCount Int
deriving (Show, Eq, Ord)
newtype Elf = Elf CalorieCount newtype Elf = Elf CalorieCount
deriving (Show, Eq, Ord)
instance Show CalorieCount where
show (CalorieCount c) = "Calories[" ++ show c ++ "]"
instance Show Elf where
show (Elf (CalorieCount c)) = "Elf[" ++ show c ++ "]"
instance Eq CalorieCount where
(==) (CalorieCount c1) (CalorieCount c2) = c1 == c2
instance Eq Elf where
(==) (Elf (CalorieCount c1)) (Elf (CalorieCount c2)) = c1 == c2
instance Ord Elf where
compare (Elf (CalorieCount c1)) (Elf (CalorieCount c2)) = compare c1 c2
elf :: Int -> Elf elf :: Int -> Elf
elf c = Elf (CalorieCount c) elf c = Elf (CalorieCount c)

26
test/Day1Spec.hs Normal file
View File

@@ -0,0 +1,26 @@
{-# LANGUAGE OverloadedStrings #-}
module Day1Spec (spec) where
import Test.Hspec
import Day1
import Day1.Internal
import qualified Data.Text as T
spec :: Spec
spec =
describe "Day1" $ do
it "sorts elves" $ do
let input = [ elf 6000, elf 4000, elf 11000]
sortElves input `shouldBe` [ elf 11000, elf 6000, elf 4000]
it "converts a list of strings to a list of either int or caloriecount" $ do
input <- readFile "ressources/day01-input-test"
parseElfInput (T.pack input) `shouldBe`
[ elf 6000, elf 4000, elf 11000, elf 24000, elf 10000 ]
it "parser text to int" $ do
intOrZero "1000" `shouldBe` 1000
it "finds the elf with the highest calorie count" $ do
input <- readFile "ressources/day01-input-test"
findElfWithHighestCalorieCount (T.pack input) `shouldBe` elf 24000
it "sums the calorie counts" $ do
input <- readFile "ressources/day01-input-test"
findCaloriesOfTop3Elves (T.pack input) `shouldBe` calories 45000

View File

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