From 526382de827b70731f704b8ecd49887cd238e06f Mon Sep 17 00:00:00 2001 From: Akshay Date: Thu, 17 Dec 2020 14:30:38 +0530 Subject: add initial day17 solution --- aoc.cabal | 6 ++++++ execs/Day17.hs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ input/17 | 8 ++++++++ input/17sample | 3 +++ 4 files changed, 64 insertions(+) create mode 100644 execs/Day17.hs create mode 100644 input/17 create mode 100644 input/17sample diff --git a/aoc.cabal b/aoc.cabal index 71726f4..03feb15 100644 --- a/aoc.cabal +++ b/aoc.cabal @@ -118,4 +118,10 @@ executable Day16 default-language: Haskell2010 hs-source-dirs: execs +executable Day17 + main-is: Day17.hs + build-depends: base, aoc, containers, split, parsec + default-language: Haskell2010 + hs-source-dirs: execs + 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 @@ +module Main where + +import Control.Monad +import Data.Map (Map) +import qualified Data.Map as Map +import Data.Maybe +import Utils + +type Pos = (Int, Int, Int, Int) + +toBool '#' = True +toBool '.' = False + +gridMap :: String -> Map Pos Bool +gridMap s = Map.fromList + [((x,y,0,0), toBool a) + | (y, row) <- zip [0..] rows + , (x, a) <- zip [0..] row + ] + where rows = lines s + +around :: Pos -> [Pos] +around (x,y,z,w) = + [(x+x', y+y', z+z', w+w') + | x' <- [-1..1] + , y' <- [-1..1] + , z' <- [-1..1] + , w' <- [-1..1] + , (x',y',z',w') /= (0,0,0,0) + ] + +convert True 2 = True +convert True 3 = True +convert False 3 = True +convert _ _ = False + +doStep :: Map Pos Bool -> Map Pos Bool +doStep m = Map.mapWithKey fn $ m <> Map.fromList [(p, False) | p <- concatMap around (Map.keys m)] + where fn pos v = convert v n + where n = howMany id $ map (fromMaybe False . flip Map.lookup m) $ around pos + +main :: IO () +main = do + n <- readFile "input/17" + let grid = gridMap n + print $ Map.size $ Map.filter id (iterate doStep grid !! 6) + diff --git a/input/17 b/input/17 new file mode 100644 index 0000000..e808267 --- /dev/null +++ b/input/17 @@ -0,0 +1,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 @@ +.#. +..# +### -- cgit v1.2.3