From 58fd8f9a003a3db8359ec9518f71844edaf1c7f1 Mon Sep 17 00:00:00 2001 From: sloane <1699281+sloanelybutsurely@users.noreply.github.com> Date: Sat, 9 Dec 2023 21:33:58 -0500 Subject: [PATCH] remove points --- 2023/9.hs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/2023/9.hs b/2023/9.hs index 6da8d3a..3d903af 100755 --- a/2023/9.hs +++ b/2023/9.hs @@ -1,5 +1,7 @@ #!/usr/bin/env runhaskell +import Control.Monad (ap) + parseInput :: String -> [[Integer]] parseInput = map (map read . words) . lines @@ -9,17 +11,17 @@ solve str = show (part1', part2') part1' = part1 input part2' = part2 input -part1 xs = sum $ map next xs -part2 xs = sum $ map prev xs +part1 = sum . map next +part2 = sum . map prev -deltas xs = zipWith (flip (-)) xs $ tail xs -differentiate xs = takeWhile notAllZeros $ iterate deltas xs +deltas = ap (zipWith subtract) tail +differentiate = takeWhile notAllZeros . iterate deltas where - notAllZeros xs = not $ all (== 0) xs + notAllZeros = not . all (0 ==) -next xs = sum $ map last $ differentiate xs +next = sum . map last . differentiate -prev xs = foldl1 (flip (-)) $ reverse $ map head $ differentiate xs +prev = foldl1 subtract . reverse . map head . differentiate main :: IO () main = do