remove points
This commit is contained in:
parent
49c1d798dc
commit
58fd8f9a00
1 changed files with 9 additions and 7 deletions
16
2023/9.hs
16
2023/9.hs
|
@ -1,5 +1,7 @@
|
||||||
#!/usr/bin/env runhaskell
|
#!/usr/bin/env runhaskell
|
||||||
|
|
||||||
|
import Control.Monad (ap)
|
||||||
|
|
||||||
parseInput :: String -> [[Integer]]
|
parseInput :: String -> [[Integer]]
|
||||||
parseInput = map (map read . words) . lines
|
parseInput = map (map read . words) . lines
|
||||||
|
|
||||||
|
@ -9,17 +11,17 @@ solve str = show (part1', part2')
|
||||||
part1' = part1 input
|
part1' = part1 input
|
||||||
part2' = part2 input
|
part2' = part2 input
|
||||||
|
|
||||||
part1 xs = sum $ map next xs
|
part1 = sum . map next
|
||||||
part2 xs = sum $ map prev xs
|
part2 = sum . map prev
|
||||||
|
|
||||||
deltas xs = zipWith (flip (-)) xs $ tail xs
|
deltas = ap (zipWith subtract) tail
|
||||||
differentiate xs = takeWhile notAllZeros $ iterate deltas xs
|
differentiate = takeWhile notAllZeros . iterate deltas
|
||||||
where
|
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 :: IO ()
|
||||||
main = do
|
main = do
|
||||||
|
|
Loading…
Reference in a new issue