diff options
-rw-r--r-- | aoc.cabal | 6 | ||||
-rw-r--r-- | execs/Day10.hs | 29 | ||||
-rw-r--r-- | input/10 | 92 | ||||
-rw-r--r-- | lib/Utils.hs | 6 |
4 files changed, 133 insertions, 0 deletions
@@ -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 | |||
79 | executable 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 | |||
2 | module Main where | ||
3 | |||
4 | import Utils | ||
5 | import Data.List (sort) | ||
6 | import Control.Monad | ||
7 | import Control.Monad.Memo | ||
8 | |||
9 | parseLine :: String -> Int | ||
10 | parseLine = read | ||
11 | |||
12 | diffs :: [Int] -> Int | ||
13 | diffs s = product $ ($ q s) <$> [howMany (==1), howMany (==3)] | ||
14 | where q = zipWith subtract `ap` tail | ||
15 | |||
16 | combos 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 | |||
23 | main :: IO () | ||
24 | main = 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 @@ | |||
1 | 56 | ||
2 | 139 | ||
3 | 42 | ||
4 | 28 | ||
5 | 3 | ||
6 | 87 | ||
7 | 142 | ||
8 | 57 | ||
9 | 147 | ||
10 | 6 | ||
11 | 117 | ||
12 | 95 | ||
13 | 2 | ||
14 | 112 | ||
15 | 107 | ||
16 | 54 | ||
17 | 146 | ||
18 | 104 | ||
19 | 40 | ||
20 | 26 | ||
21 | 136 | ||
22 | 127 | ||
23 | 111 | ||
24 | 47 | ||
25 | 8 | ||
26 | 24 | ||
27 | 13 | ||
28 | 92 | ||
29 | 18 | ||
30 | 130 | ||
31 | 141 | ||
32 | 37 | ||
33 | 81 | ||
34 | 148 | ||
35 | 31 | ||
36 | 62 | ||
37 | 50 | ||
38 | 80 | ||
39 | 91 | ||
40 | 33 | ||
41 | 77 | ||
42 | 1 | ||
43 | 96 | ||
44 | 100 | ||
45 | 9 | ||
46 | 120 | ||
47 | 27 | ||
48 | 97 | ||
49 | 60 | ||
50 | 102 | ||
51 | 25 | ||
52 | 83 | ||
53 | 55 | ||
54 | 118 | ||
55 | 19 | ||
56 | 113 | ||
57 | 49 | ||
58 | 133 | ||
59 | 14 | ||
60 | 119 | ||
61 | 88 | ||
62 | 124 | ||
63 | 110 | ||
64 | 145 | ||
65 | 65 | ||
66 | 21 | ||
67 | 7 | ||
68 | 74 | ||
69 | 72 | ||
70 | 61 | ||
71 | 103 | ||
72 | 20 | ||
73 | 41 | ||
74 | 53 | ||
75 | 32 | ||
76 | 44 | ||
77 | 10 | ||
78 | 34 | ||
79 | 121 | ||
80 | 114 | ||
81 | 67 | ||
82 | 69 | ||
83 | 66 | ||
84 | 82 | ||
85 | 101 | ||
86 | 68 | ||
87 | 84 | ||
88 | 48 | ||
89 | 73 | ||
90 | 17 | ||
91 | 43 | ||
92 | 140 | ||
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 | ||
44 | windows :: Int -> [a] -> [[a]] | 44 | windows :: Int -> [a] -> [[a]] |
45 | windows m = foldr (zipWith (:)) (repeat []) . take m . tails | 45 | windows m = foldr (zipWith (:)) (repeat []) . take m . tails |
46 | |||
47 | kadane :: [Int] -> Int | ||
48 | kadane = 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 | ||