aboutsummaryrefslogtreecommitdiff
path: root/execs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-12-17 09:00:38 +0000
committerAkshay <[email protected]>2020-12-17 09:00:38 +0000
commit526382de827b70731f704b8ecd49887cd238e06f (patch)
tree5eab0561f2d0ac563bea1421f63c325ec2b6522a /execs
parent28091a3489732f42bf7419c6fcc6f0a3d1c674fc (diff)
add initial day17 solution
Diffstat (limited to 'execs')
-rw-r--r--execs/Day17.hs47
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 @@
1module Main where
2
3import Control.Monad
4import Data.Map (Map)
5import qualified Data.Map as Map
6import Data.Maybe
7import Utils
8
9type Pos = (Int, Int, Int, Int)
10
11toBool '#' = True
12toBool '.' = False
13
14gridMap :: String -> Map Pos Bool
15gridMap 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
22around :: Pos -> [Pos]
23around (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
32convert True 2 = True
33convert True 3 = True
34convert False 3 = True
35convert _ _ = False
36
37doStep :: Map Pos Bool -> Map Pos Bool
38doStep 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
42main :: IO ()
43main = do
44 n <- readFile "input/17"
45 let grid = gridMap n
46 print $ Map.size $ Map.filter id (iterate doStep grid !! 6)
47