Faster day 14, with mutable unboxed arrays
[advent-of-code-23.git] / advent22 / Main.hs
index 87bdf6cb0b834c2cbd0d6cb577e28c41a49e1d1b..55b64b636a83f0d86512e03ec074e40bf46887e8 100644 (file)
@@ -12,8 +12,8 @@ import Linear
 -- import Linear.V3
 import Data.List
 import Data.Function
-import qualified Data.Map as M
-import Data.Map ((!))
+import qualified Data.Map.Strict as M
+import Data.Map.Strict ((!))
 import qualified Data.Set as S
 
 type Block = (V3 Int, V3 Int)
@@ -63,9 +63,11 @@ dropBlocks blocks = foldl' dropBlock [] blocks
 
 dropBlock :: [Block] -> Block -> [Block]
 dropBlock resting block = resting ++ [over both (^-^ (V3 0 0 fallDistance)) block]
-  where _2xy = alongside _xy _xy
-        blockPlane = block ^. _2xy
-        under = filter ((intersectsXY blockPlane) . (view _2xy)) resting
+  where -- _2xy = alongside _xy _xy
+        -- blockPlane = block ^. _2xy
+        blockPlane = over both (view _xy) block
+        -- under = filter ((intersectsXY blockPlane) . (view _2xy)) resting
+        under = filter ((intersectsXY blockPlane) . (over both (view _xy))) resting
         highestUnder = maxZ under
         fallDistance = block ^. _1 . _z - highestUnder - 1
 
@@ -124,7 +126,7 @@ blockP :: Parser Block
 vertexP :: Parser (V3 Int)
 
 blocksP = blockP `sepBy` endOfLine
-blockP = cubify <$> (vertexP <* "~") <*> vertexP
+blockP = cubify <$> vertexP <* "~" <*> vertexP
   where cubify (V3 x1 y1 z1) (V3 x2 y2 z2) = 
           ( (V3 (min x1 x2) (min y1 y2) (min z1 z2))
           , (V3 (max x1 x2) (max y1 y2) (max z1 z2))