aboutsummaryrefslogtreecommitdiff
path: root/execs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-12-11 10:19:03 +0000
committerAkshay <[email protected]>2020-12-11 10:19:03 +0000
commit42523f2455fb30181efc29e3a47799283b05fa80 (patch)
treece0687d20a0813cd8bcb2ccc46ff16a2db8b7251 /execs
parentedd8e1fa47c6895b28bbe61ab786ab6dc30471cd (diff)
add initial solution to day11
Diffstat (limited to 'execs')
-rw-r--r--execs/Day10.hs4
-rw-r--r--execs/Day11.hs48
2 files changed, 49 insertions, 3 deletions
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