mirror of
https://github.com/mx42/adventofcode.git
synced 2026-01-14 22:09:50 +01:00
WIP AOC 2022 & 2023
This commit is contained in:
39
AOC2023/src/Day09.hs
Normal file
39
AOC2023/src/Day09.hs
Normal file
@@ -0,0 +1,39 @@
|
||||
module Day09 (day09) where
|
||||
|
||||
import Data.List.Split (splitOn)
|
||||
|
||||
parseLine :: String -> [Int]
|
||||
parseLine i = map read $ splitOn " " i
|
||||
|
||||
getDifferences :: [Int] -> [Int]
|
||||
getDifferences [] = []
|
||||
getDifferences [_] = []
|
||||
getDifferences (h:t@(th:_)) = (th - h) : getDifferences t
|
||||
|
||||
listIsZeros :: [Int] -> Bool
|
||||
listIsZeros = all (== 0)
|
||||
|
||||
getIterations :: [Int] -> [[Int]]
|
||||
getIterations a = takeWhile (not . listIsZeros) $ iterate getDifferences a
|
||||
|
||||
getNextItem :: [Int] -> Int -> Int
|
||||
getNextItem l incr = last l + incr
|
||||
|
||||
getNextItems :: [[Int]] -> Int
|
||||
getNextItems = foldr getNextItem 0
|
||||
|
||||
getPrevItem :: [Int] -> Int -> Int
|
||||
getPrevItem l incr = head l - incr
|
||||
|
||||
getPrevItems :: [[Int]] -> Int
|
||||
getPrevItems = foldr getPrevItem 0
|
||||
|
||||
day09 :: IO ()
|
||||
day09 = do
|
||||
putStrLn "AOC 2023 day 09"
|
||||
input <- getContents
|
||||
let input' = map parseLine $ lines input
|
||||
putStrLn "Part1"
|
||||
print $ sum $ map (getNextItems . getIterations) input'
|
||||
putStrLn "Part2"
|
||||
print $ sum $ map (getPrevItems . getIterations) input'
|
||||
Reference in New Issue
Block a user