-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day09.curry
30 lines (26 loc) · 933 Bytes
/
Day09.curry
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import System.Environment (getArgs)
extrapolate :: [Int] -> Int
extrapolate = extrapolate' . reverse
where
extrapolate' :: [Int] -> Int
extrapolate' xs | all (== 0) xs = 0
| otherwise = head xs + extrapolate' (diffs xs)
diffs :: [Int] -> [Int]
diffs xs = zipWith (-) xs (drop 1 xs)
result :: [[Int]] -> Int
result = foldr (+) 0 . (extrapolate <$>)
main :: IO ()
main = do
args <- getArgs
case args of
[path] -> do
raw <- readFile path
-- We need to read the input strictly to avoid accidentally mixing lazy IO
-- and nondeterminism (which would lead to bogus results and end-of-stream
-- errors).
let input = (read <$>) . words <$> (lines $!! raw)
part1 = result input
part2 = result $ reverse <$> input
putStrLn $ "Part 1: " ++ show part1
putStrLn $ "Part 2: " ++ show part2
_ -> putStrLn "Usage: day09 <input>"