aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--aoc.cabal10
-rw-r--r--execs/Day10.hs4
-rw-r--r--execs/Day11.hs48
-rw-r--r--input/1194
-rw-r--r--input/11sample10
-rw-r--r--input/11sample29
-rw-r--r--lib/Utils.hs44
7 files changed, 198 insertions, 21 deletions
diff --git a/aoc.cabal b/aoc.cabal
index bcb8d8c..cd5d039 100644
--- a/aoc.cabal
+++ b/aoc.cabal
@@ -19,8 +19,8 @@ extra-source-files: CHANGELOG.md
19library 19library
20 hs-source-dirs: lib 20 hs-source-dirs: lib
21 default-language: Haskell2010 21 default-language: Haskell2010
22 build-depends: base 22 build-depends: base, containers
23 exposed-modules: Utils 23 exposed-modules: Utils
24 24
25executable Day01 25executable Day01
26 main-is: Day01.hs 26 main-is: Day01.hs
@@ -81,3 +81,9 @@ executable Day10
81 build-depends: base, aoc, containers, monad-memo 81 build-depends: base, aoc, containers, monad-memo
82 default-language: Haskell2010 82 default-language: Haskell2010
83 hs-source-dirs: execs 83 hs-source-dirs: execs
84
85executable Day11
86 main-is: Day11.hs
87 build-depends: base, aoc, containers
88 default-language: Haskell2010
89 hs-source-dirs: execs
diff --git a/execs/Day10.hs b/execs/Day10.hs
index c42a550..40d3d61 100644
--- a/execs/Day10.hs
+++ b/execs/Day10.hs
@@ -16,9 +16,7 @@ diffs s = product $ ($ q s) <$> [howMany (==1), howMany (==3)]
16combos top s = startEvalMemo $ go 0 16combos top s = startEvalMemo $ go 0
17 where go c 17 where go c
18 | c == top = return 1 18 | c == top = return 1
19 | otherwise = do 19 | otherwise = sum <$> mapM (memo go) (filter (`elem` s) $ map (+c) [1,2,3])
20 let cs = filter (`elem` s) $ map (+c) [1,2,3]
21 sum <$> mapM (memo go) cs
22 20
23main :: IO () 21main :: IO ()
24main = do 22main = do
diff --git a/execs/Day11.hs b/execs/Day11.hs
new file mode 100644
index 0000000..e2f138b
--- /dev/null
+++ b/execs/Day11.hs
@@ -0,0 +1,48 @@
1module Main where
2
3import Utils
4import Data.List (sortOn)
5import Data.Map (Map, (!))
6import qualified Data.Map as Map
7import Data.Maybe
8
9dirs = [ (-1,-1), (0,-1), (1,-1)
10 , (-1, 0), (1, 0)
11 , (-1, 1), (0, 1), (1, 1)
12 ]
13
14adjs1 m pt = howMany (== '#') $ mapMaybe ((`Map.lookup` m) . add pt) dirs
15
16inGrid (x, y) w h = x < w && x >= 0 && y < h && y >= 0
17adjs2 grid pt w h = howMany ((== '#') . head)
18 $ filter (not . null)
19 $ map (dropWhile (== '.')
20 . map (grid !)
21 . takeWhile (inside (0, 0) (w-1, h-1))
22 . map ($ pt)
23 . repeatF
24 . add)
25 dirs
26
27rule1 w h m pt@(x, y) = if as == 0 then '#' else 'L'
28 where as = adjs2 m pt w h
29
30rule2 w h m pt@(x, y) = if as >= 5 then 'L' else '#'
31 where as = adjs2 m pt w h
32
33doStep w h m = Map.mapWithKey fn m
34 where fn k 'L' = rule1 w h m k
35 fn k '#' = rule2 w h m k
36 fn k '.' = '.'
37
38stepWhile prev w h
39 | prev == next = next
40 | otherwise = stepWhile next w h
41 where next = doStep w h prev
42
43main :: IO ()
44main = do
45 n <- readFile "input/11"
46 let (grid, width, height) = makeGrid n
47 let solve1 = stepWhile grid width height
48 print $ howMany ((== '#') . snd) $ Map.toList solve1
diff --git a/input/11 b/input/11
new file mode 100644
index 0000000..5a109fd
--- /dev/null
+++ b/input/11
@@ -0,0 +1,94 @@
1LLLLLL.LLLLLLLLLLLLLLLL.LLLLLLLL.LLLLLLLLLLLLLLLLLLL.LLLLL.LLLLLLL.LL.L.LLLL.LLLLLLLLL.LLLLL
2LLLLLLLL.LLL.LLLL.LLLLL.LLLLLLLL.LLLLLLLLL.LLLLLLLLLLLLLLL.LLLLLLL.LLLL.LLLLLLLLL.LLLLLLLLLL
3LLLLLLLLLLLLLLLLL.LLLLL.L.LLLLLL..LLLLLLLLLLLLLLLLLL.LLLLL.LLLLLLL.LLLLLLLLL.LLLL.LLLL.LLLLL
4LLL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LLLLL.LLLLLLL.LLLL.LLLL.LLLL.LLLLLLLL.L
39LL.L...LL....L..L.L..LL.....L.......L.LL...L.LLLL..L...............L.L..L.L...L..L..L..L....
40LLLLLL.LLLLLLLLLL.L.LLL.LLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLL.LLLL.LLLLLLLLLLLLLL.LLLLL
41LLLLLLLLLLLL.LLLL.LLLLL.LLLLLLLL.LLLLLLLL..LLLLLLLLL.LL..L.LLLLLLL.LLLL.LLLL.LLLL.LLLL..LLLL
42LLLLLL.LLLLL.LLLL.LLLLL.LLLLLLLL.LLLLLLLLL.LLLLLLLLLLLLLLL.LLLLLLLLLLLL.LLLL.LLLL.LLLL.LLLLL
43LLLLLL.LLLLL.LLLL.LLLLL.LLLLLLLL.LLLLLLLLL.LLLLLLLL.LLLLLL.LLLLLLL.LLLL.LLLL.LLLLLLLLLLLLLLL
44LLLLLL.LLLLL.LLLLLLLLLL.LLL.LLLL.LLLLL.LLL.LLLLLLLLL.LLLLL.LLLLLLL.LLLL.LLLL.LLLL.LLLLLLLLLL
45LLLLLLLLLLLL.LLLL.LLLLL.LLLL.LLL.LLLLLLLLLLLLLLLLLLL.LLLLL.LLLLLLL.LLLL.LLLL.LLLL.LLLL.LLLLL
46LLLLL.LLLL.L.LLLL.LLLLL.LLLLLLLL.LLLLLLLLLLLLLLLLLLL.LLLLL.LLLLLLL.LLLL.LLLL.LLLL.LLLLLLLLLL
47.LL...LL...L...L..L..........L..L.L..L...LL......LL......L.......LL.LL..L...LLL.......LLLL..
48LLLLLLLLLLLL.LLLL.LLLLLLL.LLLLLL.LLLLLLLLL.LLLLLLLL...LLLL.LLLLLLL.LLLL..LLL.LLLL.LLLL.LLLLL
49LLLLLL.LLLLLLLLLLLLLLLL.LLLLLLLL.LLLLLLLLLLLLLLLLLLL.LL.L..LLLLLLL.LLLL.LLLL.LLLL.LLLL.LLLLL
50LLLLLLLLLLL..LLLL.LLLLL.LLLLLLLL.LLLLLLL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LL.LLLLL
85.LL.L.L.L.LL.....L.L.LL....L..L.............L...L.L...LLL.LL.L..L........L.....L.LL..L..L...
86LLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLLLLLLLLL.LLLLL.L.LLLLLLLLL.LLLL.LLLLLLLLLL
87LLLLLL.LLLLL.LL.LLL.LLL.LLLLLLLL.LLLLLLLLL.LLLLLLLLL.LLLLL.LLLLLLL.LLLLLLLLL.L.LLLLLLL.LLLLL
88L.LLLL.LL.LL.LLLL.LLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLLL.LLLLL.LLLLLLL.LLLL.LLLL.LLLL.LLLL.LLLLL
89LLLLLL.LLLLLLLLLL.LLLLL.LLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLL.LLLLLLLLL.LLLL..LLLL
90LLLLLL.LLLLLLLLLL.LLLLL.LLLLLLLL.LLLLLLLLL.LLLLLLLLL.LLLLL.LLLLLLLLLLLL.LLLL.LLLLLLL.L.LLLLL
91LLLLLL.LLLLL.LLLLLLLLLL.LLLLLLLL.LLLLLLLL.LLLLLLLLLL.LLLLLLLLLLLLL.LLLL.LLLL.LLLL.LLLL.LLLLL
92LL.LLLLLLLLL.LLLL.LLLLL.LLLLLLLL.LLLLLLLLL..LLLLLLLL.LLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLL.LLLLL
93LLLLLL.LLLLL.LLLL.LLLLL.L.LLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLL.LLLLLLL.LLLL.LLLL.LLLLLLLLLLLLLLL
94LLLLLLLLLLLL.LLLL.LLLLL.LLLLLLLL.LL.LLLLLLL.LLLLLLLL.LLLLL.LLLLLLLLLLLL.LLLL.LLLLLLLLL.LLLLL
diff --git a/input/11sample b/input/11sample
new file mode 100644
index 0000000..1beaede
--- /dev/null
+++ b/input/11sample
@@ -0,0 +1,10 @@
1L.LL.LL.LL
2LLLLLLL.LL
3L.L.L..L..
4LLLL.LL.LL
5L.LL.LL.LL
6L.LLLLL.LL
7..L.L.....
8LLLLLLLLLL
9L.LLLLLL.L
10L.LLLLL.LL
diff --git a/input/11sample2 b/input/11sample2
new file mode 100644
index 0000000..a486e35
--- /dev/null
+++ b/input/11sample2
@@ -0,0 +1,9 @@
1.......#.
2...#.....
3.#.......
4.........
5..#L....#
6....#....
7.........
8#........
9...#.....
diff --git a/lib/Utils.hs b/lib/Utils.hs
index 61c4e49..6358230 100644
--- a/lib/Utils.hs
+++ b/lib/Utils.hs
@@ -1,19 +1,11 @@
1module Utils ( binaryToInt 1module Utils where
2 , countElem
3 , xor
4 , right
5 , bet
6 , (&+)
7 , howMany
8 , sublists
9 , windows
10 ) where
11
12import Data.Char (digitToInt)
13import Control.Monad
14import Data.Either
15import Data.List (inits, tails)
16 2
3import Data.Char (digitToInt)
4import Control.Monad
5import Data.Either
6import Data.List (inits, tails)
7import Data.Map (Map)
8import qualified Data.Map as Map
17 9
18binaryToInt :: String -> Int 10binaryToInt :: String -> Int
19binaryToInt = foldl (\a x -> a * 2 + digitToInt x) 0 11binaryToInt = foldl (\a x -> a * 2 + digitToInt x) 0
@@ -28,13 +20,16 @@ right :: Either a b -> b
28right (Right b) = b 20right (Right b) = b
29right _ = undefined 21right _ = undefined
30 22
31bet :: Int -> (Int, Int) -> Bool
32bet k (l, u) = k >= l && k <= u 23bet k (l, u) = k >= l && k <= u
24bet' k (l, u) = k > l && k < u
33 25
34-- combine filter predicates 26-- combine filter predicates
35(&+) :: (a -> Bool) -> (a -> Bool) -> (a -> Bool) 27(&+) :: (a -> Bool) -> (a -> Bool) -> (a -> Bool)
36(&+) = liftM2 (&&) 28(&+) = liftM2 (&&)
37 29
30(|+) :: (a -> Bool) -> (a -> Bool) -> (a -> Bool)
31(|+) = liftM2 (||)
32
38howMany :: (a -> Bool) -> [a] -> Int 33howMany :: (a -> Bool) -> [a] -> Int
39howMany predicate = length . filter predicate 34howMany predicate = length . filter predicate
40 35
@@ -49,3 +44,20 @@ kadane = go 0 0
49 where go :: Int -> Int -> [Int] -> Int 44 where go :: Int -> Int -> [Int] -> Int
50 go best _ [] = best 45 go best _ [] = best
51 go best current (l:ls) = go (max best (current + l)) (max current (current + l)) ls 46 go best current (l:ls) = go (max best (current + l)) (max current (current + l)) ls
47
48-- tuple stuff
49
50add (x, y) (a, b) = (x+a, y+b)
51shear c (x, y) = (c * x, c * y)
52inside (p, q) (r, s) (a, b) = bet a (p, r) && bet b (q, s)
53inside' (p, q) (r, s) (a, b) = bet' a (p, r) && bet' b (q, s)
54
55-- [f, f.f, f.f.f, ...]
56repeatF f = f : map (f .) (repeatF f)
57
58makeGrid :: String -> (Map (Int, Int) Char, Int, Int)
59makeGrid s = (grid, width, height) where
60 rows = lines s
61 grid = Map.fromList [((x, y), a) | (y, row) <- zip [0..] rows , (x, a) <- zip [0..] row]
62 width = length (head rows)
63 height = length rows