diff --git a/aoc2022.cabal b/aoc2022.cabal index 356674b..91b52ee 100644 --- a/aoc2022.cabal +++ b/aoc2022.cabal @@ -27,6 +27,7 @@ library exposed-modules: Day1 Day1.Internal + Day2 Lib other-modules: Paths_aoc2022 @@ -61,6 +62,8 @@ test-suite aoc2022-test type: exitcode-stdio-1.0 main-is: Spec.hs other-modules: + Day1Spec + Day2Spec Paths_aoc2022 hs-source-dirs: test diff --git a/src/Day1/Internal.hs b/src/Day1/Internal.hs index 208f76d..1401750 100644 --- a/src/Day1/Internal.hs +++ b/src/Day1/Internal.hs @@ -18,22 +18,10 @@ import qualified Data.List as L import Data.Either newtype CalorieCount = CalorieCount Int + deriving (Show, Eq, Ord) + newtype Elf = Elf CalorieCount - -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 + deriving (Show, Eq, Ord) elf :: Int -> Elf elf c = Elf (CalorieCount c) diff --git a/test/Day1Spec.hs b/test/Day1Spec.hs new file mode 100644 index 0000000..dafa3a8 --- /dev/null +++ b/test/Day1Spec.hs @@ -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 + diff --git a/test/Spec.hs b/test/Spec.hs index 3955677..1a68773 100644 --- a/test/Spec.hs +++ b/test/Spec.hs @@ -1,25 +1,2 @@ -{-# LANGUAGE OverloadedStrings #-} -import Test.Hspec -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 - +{-# OPTIONS_GHC -F -pgmF hspec-discover #-} +module Main () where