From b5c12d69af914e48641419063fa60f19c011724f Mon Sep 17 00:00:00 2001 From: Alex Jercan Date: Sat, 9 Dec 2023 09:23:33 +0200 Subject: [PATCH] day 9 --- aoc.sh | 3 +++ aoc2023.cabal | 1 + src/Day09.hs | 28 ++++++++++++++++++++++++++-- test/Main.hs | 2 ++ test/TestDay09.hs | 22 ++++++++++++++++++++++ 5 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 test/TestDay09.hs diff --git a/aoc.sh b/aoc.sh index be4c12c..90698a7 100755 --- a/aoc.sh +++ b/aoc.sh @@ -51,3 +51,6 @@ stack exec day07 < input/day07.input echo -e "${IRed}--- Day 8: Haunted Wasteland ---${Color_Off}" stack exec day08 < input/day08.input + +echo -e "${IYellow}--- Day 9: Mirage Maintenance ---${Color_Off}" +stack exec day09 < input/day09.input diff --git a/aoc2023.cabal b/aoc2023.cabal index 6bec9c5..e3b7c5d 100644 --- a/aoc2023.cabal +++ b/aoc2023.cabal @@ -361,6 +361,7 @@ test-suite spec TestDay06 TestDay07 TestDay08 + TestDay09 Paths_aoc2023 autogen-modules: Paths_aoc2023 diff --git a/src/Day09.hs b/src/Day09.hs index 8a125e5..b624199 100644 --- a/src/Day09.hs +++ b/src/Day09.hs @@ -1,10 +1,34 @@ module Day09 (main, part1, part2) where +parse :: String -> [[Int]] +parse = map (map read . words) . lines + +diff :: [Int] -> [Int] +diff = zipWith (-) <$> tail <*> id + +prediction :: [Int] -> Int +prediction xs = fst $ until stop step (0, xs) + where + step :: (Int, [Int]) -> (Int, [Int]) + step (p, ys) = (p + last ys, diff ys) + stop :: (Int, [Int]) -> Bool + stop (_, ys) = all (== 0) ys + part1 :: String -> String -part1 = id +part1 = show . sum . map prediction . parse + +prediction' :: [Int] -> Int +prediction' xs = foldl merge 0 $ fst $ until stop step ([], xs) + where + step :: ([Int], [Int]) -> ([Int], [Int]) + step (ps, ys) = (head ys : ps, diff ys) + stop :: ([Int], [Int]) -> Bool + stop (_, ys) = all (== 0) ys + merge :: Int -> Int -> Int + merge acc x = x - acc part2 :: String -> String -part2 = const "" +part2 = show . sum . map prediction' . parse solve :: String -> String solve input = "Part 1: " ++ part1 input ++ "\nPart 2: " ++ part2 input ++ "\n" diff --git a/test/Main.hs b/test/Main.hs index 7fd2d19..549e9c2 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -8,6 +8,7 @@ import TestDay05 import TestDay06 import TestDay07 import TestDay08 +import TestDay09 main :: IO () main = do @@ -19,3 +20,4 @@ main = do TestDay06.test TestDay07.test TestDay08.test + TestDay09.test diff --git a/test/TestDay09.hs b/test/TestDay09.hs new file mode 100644 index 0000000..ec9d82d --- /dev/null +++ b/test/TestDay09.hs @@ -0,0 +1,22 @@ +module TestDay09 (test) where + +import Day09 +import Test.Hspec + +input :: String +input = "0 3 6 9 12 15\n 1 3 6 10 15 21\n10 13 16 21 30 45" + +test1 :: Expectation +test1 = part1 input `shouldBe` "114" + +test2 :: Expectation +test2 = part2 input `shouldBe` "2" + +test :: IO () +test = hspec $ do + describe "day09" $ do + describe "part1" $ do + it "should work for the examples" test1 + + describe "part2" $ do + it "should work for the examples" test2