diff options
-rw-r--r-- | aoc.cabal | 6 | ||||
-rw-r--r-- | execs/Day17.hs | 47 | ||||
-rw-r--r-- | input/17 | 8 | ||||
-rw-r--r-- | input/17sample | 3 |
4 files changed, 64 insertions, 0 deletions
@@ -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 | ||
121 | executable 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 @@ | |||
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 | |||
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 | ### | ||