[hs] Solve 2020_17 part 2

This commit is contained in:
Joscha 2020-12-18 17:16:11 +00:00
parent ecf8c1f7b7
commit 759ce027ab

View file

@ -1,5 +1,4 @@
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TupleSections #-}
module Aoc.Y2020.D17 module Aoc.Y2020.D17
( day ( day
@ -13,19 +12,21 @@ import Aoc.Parse
parser :: Parser [[Bool]] parser :: Parser [[Bool]]
parser = manyLines $ many $ (False <$ char '.') <|> (True <$ char '#') parser = manyLines $ many $ (False <$ char '.') <|> (True <$ char '#')
type Pos = (Int, Int, Int) type Pos = [Int]
type World = Set.Set Pos type World = Set.Set Pos
newWorld :: [[Bool]] -> World newWorld :: Int -> [[Bool]] -> World
newWorld newWorld dims slice = Set.fromList $ do
= Set.fromList (y, row) <- zip [0..] slice
. map fst (x, True) <- zip [0..] row
. filter snd pure $ [x, y] ++ replicate (dims - 2) 0
. concat
. zipWith (\y -> zipWith (\x -> ((x, y, 0),)) [0..]) [0..]
vicinity :: Pos -> [Pos] vicinity :: Pos -> [Pos]
vicinity (x, y, z) = (,,) <$> [x - 1, x, x + 1] <*> [y - 1, y, y + 1] <*> [z - 1, z, z + 1] vicinity [] = [[]]
vicinity (x:xs) = do
x2 <- [x - 1, x, x + 1]
xs2 <- vicinity xs
pure $ x2 : xs2
neighbours :: Pos -> [Pos] neighbours :: Pos -> [Pos]
neighbours p = [p2 | p2 <- vicinity p, p2 /= p] neighbours p = [p2 | p2 <- vicinity p, p2 /= p]
@ -50,8 +51,11 @@ steps = foldr (.) id $ replicate 6 step
solver :: [[Bool]] -> IO () solver :: [[Bool]] -> IO ()
solver slice = do solver slice = do
putStrLn ">> Part 1" putStrLn ">> Part 1"
let world = newWorld slice print $ Set.size $ steps $ newWorld 3 slice
print $ Set.size $ steps world
putStrLn ""
putStrLn ">> Part 2"
print $ Set.size $ steps $ newWorld 4 slice
day :: Day day :: Day
day = dayParse parser solver day = dayParse parser solver