1
0
Fork 0
advent-of-code/2023/9.hs
2023-12-09 21:36:01 -05:00

27 lines
565 B
Haskell
Executable file

#!/usr/bin/env runhaskell
import Control.Monad (ap)
parseInput = map (map read . words) . lines
solve str = show (part1', part2')
where
input = parseInput str
part1' = part1 input
part2' = part2 input
part1 = sum . map next
part2 = sum . map prev
deltas = ap (zipWith subtract) tail
differentiate = takeWhile notAllZeros . iterate deltas
where
notAllZeros = not . all (0 ==)
next = sum . map last . differentiate
prev = foldl1 subtract . reverse . map head . differentiate
main = do
contents <- getContents
putStrLn $ solve contents