aboutsummaryrefslogtreecommitdiff
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
parent28091a3489732f42bf7419c6fcc6f0a3d1c674fc (diff)
add initial day17 solution
-rw-r--r--aoc.cabal6
-rw-r--r--execs/Day17.hs47
-rw-r--r--input/178
-rw-r--r--input/17sample3
4 files changed, 64 insertions, 0 deletions
diff --git a/aoc.cabal b/aoc.cabal
index 71726f4..03feb15 100644
--- a/aoc.cabal
+++ b/aoc.cabal
@@ -118,4 +118,10 @@ executable Day16
118 default-language: Haskell2010 118 default-language: Haskell2010
119 hs-source-dirs: execs 119 hs-source-dirs: execs
120 120
121executable Day17
122 main-is: Day17.hs
123 build-depends: base, aoc, containers, split, parsec
124 default-language: Haskell2010
125 hs-source-dirs: execs
126
121 127
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
diff --git a/input/17 b/input/17
new file mode 100644
index 0000000..e808267
--- /dev/null
+++ b/input/17
@@ -0,0 +1,8 @@
1.#..####
2.#.#...#
3#..#.#.#
4###..##.
5..##...#
6..##.###
7#.....#.
8..##..##
diff --git a/input/17sample b/input/17sample
new file mode 100644
index 0000000..eedd3d2
--- /dev/null
+++ b/input/17sample
@@ -0,0 +1,3 @@
1.#.
2..#
3###