diff --git a/advent-of-code-2023.cabal b/advent-of-code-2023.cabal index dfc04e5..7bec955 100644 --- a/advent-of-code-2023.cabal +++ b/advent-of-code-2023.cabal @@ -68,3 +68,11 @@ executable day08 build-depends: base ^>=4.15.1.0, hashable, unordered-containers hs-source-dirs: day08 default-language: Haskell2010 + +executable day09 + main-is: Main.hs + other-modules: Commons Part1 Part2 + + build-depends: base ^>=4.15.1.0, MissingH + hs-source-dirs: day09 + default-language: Haskell2010 diff --git a/day09/Commons.hs b/day09/Commons.hs new file mode 100644 index 0000000..f4dbccb --- /dev/null +++ b/day09/Commons.hs @@ -0,0 +1,26 @@ +module Commons where + +import GHC.IO.Handle (isEOF) +import Data.List.Utils (split) + + +type History = [Int] + + +parse :: IO [History] +parse = do done <- isEOF + if done + then return [] + else do line <- getLine + let history = map read $ split " " line + otherHistories <- parse + return $ history: otherHistories + + +getDifferences :: History -> History +getDifferences [h] = [] +getDifferences (h1: h2: t) = h1 - h2: getDifferences (h2: t) + +getNextValue :: History -> Int +getNextValue (0: 0: _) = 0 +getNextValue (h: t) = h + (getNextValue . getDifferences) (h: t) diff --git a/day09/Main.hs b/day09/Main.hs new file mode 100644 index 0000000..26432f9 --- /dev/null +++ b/day09/Main.hs @@ -0,0 +1,12 @@ +module Main where + +import Commons +import qualified Part1 +import qualified Part2 + + +main = do histories <- parse + let part1Res = sum $ Part1.getNextValues histories + print part1Res + let part2Res = sum $ Part2.getNextValues histories + print part2Res diff --git a/day09/Part1.hs b/day09/Part1.hs new file mode 100644 index 0000000..10babc2 --- /dev/null +++ b/day09/Part1.hs @@ -0,0 +1,7 @@ +module Part1 where + +import Commons + + +getNextValues :: [History] -> [Int] +getNextValues = map $ getNextValue . reverse diff --git a/day09/Part2.hs b/day09/Part2.hs new file mode 100644 index 0000000..45bc989 --- /dev/null +++ b/day09/Part2.hs @@ -0,0 +1,7 @@ +module Part2 where + +import Commons + + +getNextValues :: [History] -> [Int] +getNextValues = map getNextValue