From eb457fcd464f28978d3293abecf9c6002767cba3 Mon Sep 17 00:00:00 2001 From: Manu Phatak Date: Sat, 26 Dec 2020 15:05:39 -0800 Subject: [PATCH] Harvest parseInts util (#33) --- src/Advent/Utils.hs | 3 +++ src/Day09/Solution.hs | 8 ++++---- src/Day09/Utils.hs | 5 ----- test/Advent/UtilsSpec.hs | 5 +++++ test/Day09/SolutionSpec.hs | 4 ++-- test/Day09/UtilsSpec.hs | 5 +---- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Advent/Utils.hs b/src/Advent/Utils.hs index 305feff..1c3212f 100644 --- a/src/Advent/Utils.hs +++ b/src/Advent/Utils.hs @@ -15,6 +15,9 @@ occurrences target = foldr go 0 readInt :: String -> Int readInt n = read n :: Int +parseInts :: String -> [Int] +parseInts = map readInt . lines + rightToMaybe :: Either a b -> Maybe b rightToMaybe = either (const Nothing) Just diff --git a/src/Day09/Solution.hs b/src/Day09/Solution.hs index 99bcec5..011f060 100644 --- a/src/Day09/Solution.hs +++ b/src/Day09/Solution.hs @@ -1,18 +1,18 @@ module Day09.Solution (part1, part2, xmasCypher, encryptionWeakness, rollingChunks') where -import Advent.Utils (combinations) +import Advent.Utils (combinations, parseInts) import Data.Foldable (find) import Data.Maybe (fromJust) -import Day09.Utils (parseNumbers, rollingChunks) +import Day09.Utils (rollingChunks) part1 :: String -> String part1 = show . fromJust . xmasCypher 25 part2 :: String -> String -part2 = show . (encryptionWeakness <$> (fromJust . xmasCypher 25) <*> parseNumbers) +part2 = show . (encryptionWeakness <$> (fromJust . xmasCypher 25) <*> parseInts) xmasCypher :: Int -> String -> Maybe Int -xmasCypher size = fmap snd . find (uncurry followsPreamble) . rollingChunks' size . parseNumbers +xmasCypher size = fmap snd . find (uncurry followsPreamble) . rollingChunks' size . parseInts where followsPreamble :: [Int] -> Int -> Bool followsPreamble preamble target = not $ any ((== target) . sum) (combinations 2 preamble) diff --git a/src/Day09/Utils.hs b/src/Day09/Utils.hs index bd8038f..aaae483 100644 --- a/src/Day09/Utils.hs +++ b/src/Day09/Utils.hs @@ -1,10 +1,5 @@ module Day09.Utils where -import Advent.Utils (readInt) - -parseNumbers :: String -> [Int] -parseNumbers = map readInt . lines - rollingChunks :: Int -> [a] -> [[a]] rollingChunks size xs = [ take size . drop i $ xs diff --git a/test/Advent/UtilsSpec.hs b/test/Advent/UtilsSpec.hs index c743fec..80c1d48 100644 --- a/test/Advent/UtilsSpec.hs +++ b/test/Advent/UtilsSpec.hs @@ -5,6 +5,7 @@ import Advent.Utils fromRightOrShowError, isBetween, occurrences, + parseInts, readInt, rightToMaybe, ) @@ -32,6 +33,10 @@ spec = parallel $ do it "is an int" $ readInt "123" `shouldBe` 123 + describe "parseInts" $ do + it "parses a string into numbers" $ + parseInts "1\n3\n5\n7\n11\n13\n17\n" `shouldBe` [1, 3, 5, 7, 11, 13, 17] + describe "isBetween" $ context "given a range of 1 and 13" $ let lower = 1 :: Int diff --git a/test/Day09/SolutionSpec.hs b/test/Day09/SolutionSpec.hs index ea55d33..cc95c03 100644 --- a/test/Day09/SolutionSpec.hs +++ b/test/Day09/SolutionSpec.hs @@ -1,7 +1,7 @@ module Day09.SolutionSpec (spec) where +import Advent.Utils (parseInts) import Day09.Solution (encryptionWeakness, part1, part2, rollingChunks', xmasCypher) -import Day09.Utils (parseNumbers) import Test.Hspec spec :: Spec @@ -18,7 +18,7 @@ spec = parallel $ do xmasCypher 5 input `shouldBe` Just 127 describe "encryptionWeakness" $ do it "finds the contiguous set of numbers that sum to the target" $ do - input <- parseNumbers <$> readFile "./test/Day09/example.txt" + input <- parseInts <$> readFile "./test/Day09/example.txt" encryptionWeakness 127 input `shouldBe` 62 describe "rollingChunks'" $ do it "iterates through a list for case 1" $ do diff --git a/test/Day09/UtilsSpec.hs b/test/Day09/UtilsSpec.hs index dec7398..ce98738 100644 --- a/test/Day09/UtilsSpec.hs +++ b/test/Day09/UtilsSpec.hs @@ -1,6 +1,6 @@ module Day09.UtilsSpec (spec) where -import Day09.Utils (parseNumbers, rollingChunks) +import Day09.Utils (rollingChunks) import Test.Hspec spec :: Spec @@ -10,6 +10,3 @@ spec = parallel $ do rollingChunks 2 "abcd" `shouldBe` ["ab", "bc", "cd"] it "iterates through a list for case 2" $ rollingChunks 3 "abcdef" `shouldBe` ["abc", "bcd", "cde", "def"] - describe "parseNumbers" $ do - it "parses a string into numbers" $ - parseNumbers "1\n3\n5\n7\n11\n13\n17\n" `shouldBe` [1, 3, 5, 7, 11, 13, 17]