diff options
Diffstat (limited to 'execs')
-rw-r--r-- | execs/Day10.hs | 4 | ||||
-rw-r--r-- | execs/Day11.hs | 48 |
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)] | |||
16 | combos top s = startEvalMemo $ go 0 | 16 | combos 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 | ||
23 | main :: IO () | 21 | main :: IO () |
24 | main = do | 22 | 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 @@ | |||
1 | module Main where | ||
2 | |||
3 | import Utils | ||
4 | import Data.List (sortOn) | ||
5 | import Data.Map (Map, (!)) | ||
6 | import qualified Data.Map as Map | ||
7 | import Data.Maybe | ||
8 | |||
9 | dirs = [ (-1,-1), (0,-1), (1,-1) | ||
10 | , (-1, 0), (1, 0) | ||
11 | , (-1, 1), (0, 1), (1, 1) | ||
12 | ] | ||
13 | |||
14 | adjs1 m pt = howMany (== '#') $ mapMaybe ((`Map.lookup` m) . add pt) dirs | ||
15 | |||
16 | inGrid (x, y) w h = x < w && x >= 0 && y < h && y >= 0 | ||
17 | adjs2 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 | |||
27 | rule1 w h m pt@(x, y) = if as == 0 then '#' else 'L' | ||
28 | where as = adjs2 m pt w h | ||
29 | |||
30 | rule2 w h m pt@(x, y) = if as >= 5 then 'L' else '#' | ||
31 | where as = adjs2 m pt w h | ||
32 | |||
33 | doStep 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 | |||
38 | stepWhile prev w h | ||
39 | | prev == next = next | ||
40 | | otherwise = stepWhile next w h | ||
41 | where next = doStep w h prev | ||
42 | |||
43 | main :: IO () | ||
44 | main = 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 | ||