aboutsummaryrefslogtreecommitdiff
path: root/execs/Day11.hs
diff options
context:
space:
mode:
Diffstat (limited to 'execs/Day11.hs')
-rw-r--r--execs/Day11.hs48
1 files changed, 48 insertions, 0 deletions
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