-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.hs
67 lines (53 loc) · 1.45 KB
/
run.hs
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeApplications #-}
import AoC
import AoC.Grid
-- TODO :Cleanup
import Data.Bifunctor
import Data.Foldable
import Data.List
import Data.List.Split
import Data.Maybe
import Data.Ord
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
import Data.Sequence (Seq)
import qualified Data.Sequence as Seq
import Data.Set (Set)
import qualified Data.Set as Set
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
type N = Int
nums = map (read @N) . words
parse = nums
parseAll = map parse . lines
diffs xs = zipWith (-) (drop 1 xs) xs
solve input = go input [last input]
where go xs hist
| all (== 0) xs = hist
| otherwise =
let ds = diffs xs
in go ds (last ds:hist)
solve' input = go input [head input]
where go xs hist
| all (== 0) xs = hist
| otherwise =
let ds = diffs xs
in go ds (head ds:hist)
part1 = sum . map (sum . solve)
part2 = sum . map (foldl f 0 . solve')
where f acc v = v - acc
main :: IO ()
main = main' "input.txt"
exampleMain :: IO ()
exampleMain = main' "example.txt"
main' :: FilePath -> IO ()
main' file = do
input <- parseAll <$> readFile file
print (part1 input)
print (part2 input)