Added HashSet implementation
[advent-of-code-22.git] / advent23 / Main.hs
index 08eef6c6f4f07c0970322e1d0ba74fbb44004a33..e604a965de616a869b73a168c092fb2a6e04f898 100644 (file)
@@ -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