From 42523f2455fb30181efc29e3a47799283b05fa80 Mon Sep 17 00:00:00 2001 From: Akshay Date: Fri, 11 Dec 2020 15:49:03 +0530 Subject: add initial solution to day11 --- execs/Day10.hs | 4 +--- execs/Day11.hs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 execs/Day11.hs (limited to '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)] combos top s = startEvalMemo $ go 0 where go c | c == top = return 1 - | otherwise = do - let cs = filter (`elem` s) $ map (+c) [1,2,3] - sum <$> mapM (memo go) cs + | otherwise = sum <$> mapM (memo go) (filter (`elem` s) $ map (+c) [1,2,3]) main :: IO () main = 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 @@ +module Main where + +import Utils +import Data.List (sortOn) +import Data.Map (Map, (!)) +import qualified Data.Map as Map +import Data.Maybe + +dirs = [ (-1,-1), (0,-1), (1,-1) + , (-1, 0), (1, 0) + , (-1, 1), (0, 1), (1, 1) + ] + +adjs1 m pt = howMany (== '#') $ mapMaybe ((`Map.lookup` m) . add pt) dirs + +inGrid (x, y) w h = x < w && x >= 0 && y < h && y >= 0 +adjs2 grid pt w h = howMany ((== '#') . head) + $ filter (not . null) + $ map (dropWhile (== '.') + . map (grid !) + . takeWhile (inside (0, 0) (w-1, h-1)) + . map ($ pt) + . repeatF + . add) + dirs + +rule1 w h m pt@(x, y) = if as == 0 then '#' else 'L' + where as = adjs2 m pt w h + +rule2 w h m pt@(x, y) = if as >= 5 then 'L' else '#' + where as = adjs2 m pt w h + +doStep w h m = Map.mapWithKey fn m + where fn k 'L' = rule1 w h m k + fn k '#' = rule2 w h m k + fn k '.' = '.' + +stepWhile prev w h + | prev == next = next + | otherwise = stepWhile next w h + where next = doStep w h prev + +main :: IO () +main = do + n <- readFile "input/11" + let (grid, width, height) = makeGrid n + let solve1 = stepWhile grid width height + print $ howMany ((== '#') . snd) $ Map.toList solve1 -- cgit v1.2.3