This commit is contained in:
2023-12-09 10:04:40 +01:00
parent dbfd617fb0
commit cb79c63bb2
5 changed files with 60 additions and 0 deletions

View File

@@ -68,3 +68,11 @@ executable day08
build-depends: base ^>=4.15.1.0, hashable, unordered-containers build-depends: base ^>=4.15.1.0, hashable, unordered-containers
hs-source-dirs: day08 hs-source-dirs: day08
default-language: Haskell2010 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

26
day09/Commons.hs Normal file
View File

@@ -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)

12
day09/Main.hs Normal file
View File

@@ -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

7
day09/Part1.hs Normal file
View File

@@ -0,0 +1,7 @@
module Part1 where
import Commons
getNextValues :: [History] -> [Int]
getNextValues = map $ getNextValue . reverse

7
day09/Part2.hs Normal file
View File

@@ -0,0 +1,7 @@
module Part2 where
import Commons
getNextValues :: [History] -> [Int]
getNextValues = map getNextValue