X-Git-Url: https://git.njae.me.uk/?p=advent-of-code-22.git;a=blobdiff_plain;f=advent23%2FMain.hs;fp=advent23%2FMain.hs;h=e604a965de616a869b73a168c092fb2a6e04f898;hp=08eef6c6f4f07c0970322e1d0ba74fbb44004a33;hb=549425defbc1482abcef0e926094f0817842a4f5;hpb=c96b3e327eb7f937787e9ac846d8f7c354bfa4a1 diff --git a/advent23/Main.hs b/advent23/Main.hs index 08eef6c..e604a96 100644 --- a/advent23/Main.hs +++ b/advent23/Main.hs @@ -9,6 +9,7 @@ import Data.Monoid import Control.Monad.State.Strict import Control.Monad.ST import qualified Data.Array.IArray as A +import Data.Array.IArray ((!)) import qualified Data.Array.MArray as M import Data.Array.ST import Data.Maybe @@ -73,6 +74,13 @@ simulateN n = simulateN (n - 1) simulateOnce = + do updateGrove + growGrove + updateDirections + updateCount + +updateGrove :: GroveState () +updateGrove = do grove <- gets currentGrove proposalsInf <- gets proposalDirections let proposals = take 4 proposalsInf @@ -85,9 +93,6 @@ simulateOnce = moveElves mPopulation return mPopulation modify' (\g -> g { currentGrove = newGrove}) - growGrove - updateDirections - updateCount growGrove = do grove <- gets currentGrove @@ -206,7 +211,7 @@ shrink grove findStrip stripDirection currentBounds where shiftBounds (b0, b1) (d0, d1) = (b0 ^+^ d0, b1 ^+^ d1) emptyStrip :: Population -> (Position, Position) -> Bool -emptyStrip grove strip = all isNothing $ fmap (grove A.!) $ range strip +emptyStrip grove strip = all isNothing $ fmap (grove !) $ range strip topStrip, bottomStrip, leftStrip, rightStrip :: (Position, Position) -> (Position, Position) topStrip (V2 minX _minY, V2 maxX maxY) = (V2 minX maxY, V2 maxX maxY) @@ -221,7 +226,7 @@ leftShrink = (V2 1 0, V2 0 0) rightShrink = (V2 0 0, V2 -1 0) countEmpty :: Population -> (Position, Position) -> Int -countEmpty grove bounds = length $ filter isNothing $ fmap (grove A.!) cells +countEmpty grove bounds = length $ filter isNothing $ fmap (grove !) cells where cells = range bounds -- Parse the input file