Skip to content

Commit

Permalink
why don't we just embed this to a writer
Browse files Browse the repository at this point in the history
  • Loading branch information
Javran committed Feb 27, 2024
1 parent 4252d8a commit 3c10b11
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/Javran/AdventOfCode/Y2023/Day22.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Javran.AdventOfCode.Y2023.Day22 () where

import Control.Applicative
import Control.Monad
import Control.Monad.Writer.CPS
import Data.Char
import Data.Function
import Data.Function.Memoize (memoFix)
Expand All @@ -26,7 +27,6 @@ import qualified Data.Vector as V
import GHC.Generics (Generic)
import Javran.AdventOfCode.Prelude
import Text.ParserCombinators.ReadP hiding (count, get, many)
import Control.Monad.Writer.CPS

data Day22 deriving (Generic)

Expand Down Expand Up @@ -95,10 +95,9 @@ brickP = do

type Coord = (Int, Int) -- (x, y)
type St = IM.IntMap (M.Map Coord Int) -- z-index to 2-d coord to brick reference number.
type St' = (St, IM.IntMap Int) -- additionally tracking final z-index of each block

insertBrick :: Int -> Brick -> St' -> St'
insertBrick bkId ((x0, y0, z0), (x1, y1, z1)) (s, zs) = (IM.unionWith M.union s extra, IM.insert bkId finalZ zs)
insertBrick :: Int -> Brick -> St -> (St, IM.IntMap Int)
insertBrick bkId ((x0, y0, z0), (x1, y1, z1)) s = (IM.unionWith M.union s extra, IM.singleton bkId finalZ)
where
-- a slice of the brick (horizontally cut)
bSlice :: M.Map Coord Int
Expand Down Expand Up @@ -159,11 +158,12 @@ instance Solution Day22 where
. fmap (consumeOrDie brickP)
. lines
<$> getInputS
let (sFin, zs) = foldl' (\s (i, b) -> insertBrick i b s) mempty (zip [0 ..] xs)
let (sFin, zs) = runWriter do
foldM (\s (i, b) -> writer (insertBrick i b s)) mempty (zip [0 ..] xs)
w = execWriter do
forM_ (IM.toAscList zs) \(curBk, zBase) -> do
let bSlice = M.filter (== curBk) (sFin IM.! zBase)
case sFin IM.!? (zBase -1) of
case sFin IM.!? (zBase - 1) of
Nothing -> pure ()
Just supp -> do
let
Expand Down

0 comments on commit 3c10b11

Please sign in to comment.