[hs] Clean up 2020_17
This commit is contained in:
parent
759ce027ab
commit
ca9e4c7bf1
1 changed files with 10 additions and 16 deletions
|
|
@ -22,11 +22,7 @@ newWorld dims slice = Set.fromList $ do
|
||||||
pure $ [x, y] ++ replicate (dims - 2) 0
|
pure $ [x, y] ++ replicate (dims - 2) 0
|
||||||
|
|
||||||
vicinity :: Pos -> [Pos]
|
vicinity :: Pos -> [Pos]
|
||||||
vicinity [] = [[]]
|
vicinity = foldr (\x -> ((:) <$> [x - 1, x, x + 1] <*>)) (pure [])
|
||||||
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]
|
||||||
|
|
@ -35,27 +31,25 @@ interesting :: World -> Set.Set Pos
|
||||||
interesting w = Set.fromList $ vicinity =<< Set.toList w
|
interesting w = Set.fromList $ vicinity =<< Set.toList w
|
||||||
|
|
||||||
alive :: Bool -> Int -> Bool
|
alive :: Bool -> Int -> Bool
|
||||||
alive True 2 = True
|
alive True 2 = True
|
||||||
alive True 3 = True
|
alive _ 3 = True
|
||||||
alive False 3 = True
|
alive _ _ = False
|
||||||
alive _ _ = False
|
|
||||||
|
|
||||||
step :: World -> World
|
step :: World -> World
|
||||||
step w = Set.filter go $ interesting w
|
step w = flip Set.filter (interesting w) $ \p ->
|
||||||
where
|
alive (p `Set.member` w) (length $ filter (`Set.member` w) $ neighbours p)
|
||||||
go p = alive (p `Set.member` w) (length $ filter (`Set.member` w) $ neighbours p)
|
|
||||||
|
|
||||||
steps :: World -> World
|
steps :: Int -> World -> World
|
||||||
steps = foldr (.) id $ replicate 6 step
|
steps n = foldr (.) id $ replicate n step
|
||||||
|
|
||||||
solver :: [[Bool]] -> IO ()
|
solver :: [[Bool]] -> IO ()
|
||||||
solver slice = do
|
solver slice = do
|
||||||
putStrLn ">> Part 1"
|
putStrLn ">> Part 1"
|
||||||
print $ Set.size $ steps $ newWorld 3 slice
|
print $ Set.size $ steps 6 $ newWorld 3 slice
|
||||||
|
|
||||||
putStrLn ""
|
putStrLn ""
|
||||||
putStrLn ">> Part 2"
|
putStrLn ">> Part 2"
|
||||||
print $ Set.size $ steps $ newWorld 4 slice
|
print $ Set.size $ steps 6 $ newWorld 4 slice
|
||||||
|
|
||||||
day :: Day
|
day :: Day
|
||||||
day = dayParse parser solver
|
day = dayParse parser solver
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue