diff options
author | Akshay <[email protected]> | 2020-12-17 09:00:38 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2020-12-17 09:00:38 +0000 |
commit | 526382de827b70731f704b8ecd49887cd238e06f (patch) | |
tree | 5eab0561f2d0ac563bea1421f63c325ec2b6522a /execs | |
parent | 28091a3489732f42bf7419c6fcc6f0a3d1c674fc (diff) |
add initial day17 solution
Diffstat (limited to 'execs')
-rw-r--r-- | execs/Day17.hs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/execs/Day17.hs b/execs/Day17.hs new file mode 100644 index 0000000..bb632e0 --- /dev/null +++ b/execs/Day17.hs | |||
@@ -0,0 +1,47 @@ | |||
1 | module Main where | ||
2 | |||
3 | import Control.Monad | ||
4 | import Data.Map (Map) | ||
5 | import qualified Data.Map as Map | ||
6 | import Data.Maybe | ||
7 | import Utils | ||
8 | |||
9 | type Pos = (Int, Int, Int, Int) | ||
10 | |||
11 | toBool '#' = True | ||
12 | toBool '.' = False | ||
13 | |||
14 | gridMap :: String -> Map Pos Bool | ||
15 | gridMap s = Map.fromList | ||
16 | [((x,y,0,0), toBool a) | ||
17 | | (y, row) <- zip [0..] rows | ||
18 | , (x, a) <- zip [0..] row | ||
19 | ] | ||
20 | where rows = lines s | ||
21 | |||
22 | around :: Pos -> [Pos] | ||
23 | around (x,y,z,w) = | ||
24 | [(x+x', y+y', z+z', w+w') | ||
25 | | x' <- [-1..1] | ||
26 | , y' <- [-1..1] | ||
27 | , z' <- [-1..1] | ||
28 | , w' <- [-1..1] | ||
29 | , (x',y',z',w') /= (0,0,0,0) | ||
30 | ] | ||
31 | |||
32 | convert True 2 = True | ||
33 | convert True 3 = True | ||
34 | convert False 3 = True | ||
35 | convert _ _ = False | ||
36 | |||
37 | doStep :: Map Pos Bool -> Map Pos Bool | ||
38 | doStep m = Map.mapWithKey fn $ m <> Map.fromList [(p, False) | p <- concatMap around (Map.keys m)] | ||
39 | where fn pos v = convert v n | ||
40 | where n = howMany id $ map (fromMaybe False . flip Map.lookup m) $ around pos | ||
41 | |||
42 | main :: IO () | ||
43 | main = do | ||
44 | n <- readFile "input/17" | ||
45 | let grid = gridMap n | ||
46 | print $ Map.size $ Map.filter id (iterate doStep grid !! 6) | ||
47 | |||