aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-12-11 04:37:13 +0000
committerAkshay <[email protected]>2020-12-11 04:37:13 +0000
commitedd8e1fa47c6895b28bbe61ab786ab6dc30471cd (patch)
treeea63292c4ce916c9f34ae8ed205f33dd2d3b9d88
parentac3191ae40b8df0873f2e04f5bc1017322941b5d (diff)
add day10
-rw-r--r--aoc.cabal6
-rw-r--r--execs/Day10.hs29
-rw-r--r--input/1092
-rw-r--r--lib/Utils.hs6
4 files changed, 133 insertions, 0 deletions
diff --git a/aoc.cabal b/aoc.cabal
index 0957baa..bcb8d8c 100644
--- a/aoc.cabal
+++ b/aoc.cabal
@@ -75,3 +75,9 @@ executable Day09
75 build-depends: base, aoc, containers, parsec 75 build-depends: base, aoc, containers, parsec
76 default-language: Haskell2010 76 default-language: Haskell2010
77 hs-source-dirs: execs 77 hs-source-dirs: execs
78
79executable Day10
80 main-is: Day10.hs
81 build-depends: base, aoc, containers, monad-memo
82 default-language: Haskell2010
83 hs-source-dirs: execs
diff --git a/execs/Day10.hs b/execs/Day10.hs
new file mode 100644
index 0000000..c42a550
--- /dev/null
+++ b/execs/Day10.hs
@@ -0,0 +1,29 @@
1
2module Main where
3
4import Utils
5import Data.List (sort)
6import Control.Monad
7import Control.Monad.Memo
8
9parseLine :: String -> Int
10parseLine = read
11
12diffs :: [Int] -> Int
13diffs s = product $ ($ q s) <$> [howMany (==1), howMany (==3)]
14 where q = zipWith subtract `ap` tail
15
16combos top s = startEvalMemo $ go 0
17 where go c
18 | c == top = return 1
19 | otherwise = do
20 let cs = filter (`elem` s) $ map (+c) [1,2,3]
21 sum <$> mapM (memo go) cs
22
23main :: IO ()
24main = do
25 n <- map parseLine . lines <$> readFile "input/10"
26 let top = maximum n + 3
27 ls = sort (0:top:n)
28 print $ diffs ls
29 print $ combos top ls
diff --git a/input/10 b/input/10
new file mode 100644
index 0000000..0e77ade
--- /dev/null
+++ b/input/10
@@ -0,0 +1,92 @@
156
2139
342
428
53
687
7142
857
9147
106
11117
1295
132
14112
15107
1654
17146
18104
1940
2026
21136
22127
23111
2447
258
2624
2713
2892
2918
30130
31141
3237
3381
34148
3531
3662
3750
3880
3991
4033
4177
421
4396
44100
459
46120
4727
4897
4960
50102
5125
5283
5355
54118
5519
56113
5749
58133
5914
60119
6188
62124
63110
64145
6565
6621
677
6874
6972
7061
71103
7220
7341
7453
7532
7644
7710
7834
79121
80114
8167
8269
8366
8482
85101
8668
8784
8848
8973
9017
9143
92140
diff --git a/lib/Utils.hs b/lib/Utils.hs
index 1381f16..61c4e49 100644
--- a/lib/Utils.hs
+++ b/lib/Utils.hs
@@ -43,3 +43,9 @@ sublists = concatMap inits . tails
43 43
44windows :: Int -> [a] -> [[a]] 44windows :: Int -> [a] -> [[a]]
45windows m = foldr (zipWith (:)) (repeat []) . take m . tails 45windows m = foldr (zipWith (:)) (repeat []) . take m . tails
46
47kadane :: [Int] -> Int
48kadane = go 0 0
49 where go :: Int -> Int -> [Int] -> Int
50 go best _ [] = best
51 go best current (l:ls) = go (max best (current + l)) (max current (current + l)) ls