X-Git-Url: https://git.njae.me.uk/?p=advent-of-code-19.git;a=blobdiff_plain;f=advent24%2Fsrc%2Fadvent24.hs;fp=advent24%2Fsrc%2Fadvent24.hs;h=34cf1138bf1574b60b7bf5f2b3980dc2658361b8;hp=e784cdc05665c0ea50c6acb7aa5519703c8173b5;hb=418d5771f9b52ca7d813c1daebc91772a6e74a88;hpb=887e4c9e5607a078269338087bac136c0a143d28 diff --git a/advent24/src/advent24.hs b/advent24/src/advent24.hs index e784cdc..34cf113 100644 --- a/advent24/src/advent24.hs +++ b/advent24/src/advent24.hs @@ -6,7 +6,8 @@ import GHC.TypeNats (KnownNat) -- import Data.Functor.Compose (Compose(..)) -import Data.Matrix (Matrix, matrix, safeGet, (!), prettyMatrix, mapPos, fromList, toList) +-- import Data.Matrix (Matrix, matrix, safeGet, (!), prettyMatrix, mapPos, fromList, toList) +import qualified Data.Matrix as X import Data.Bool (bool) import Data.Distributive (Distributive(..)) import Data.Functor.Rep (Representable(..), distributeRep) @@ -17,18 +18,19 @@ import Control.Comonad (Comonad(..)) import Data.Maybe import Data.List import qualified Data.Set as S +import qualified Data.Map as M import Control.Concurrent (threadDelay) import Control.Monad (forM_) instance Ord Grid where - m1 `compare` m2 = (toList m1) `compare` (toList m2) + m1 `compare` m2 = (X.toList m1) `compare` (X.toList m2) type Coord = (Int, Int) -type Grid = Matrix Bool -type StoredGrid = Store Matrix Bool +type Grid = X.Matrix Bool +type StoredGrid = Store X.Matrix Bool type Rule = StoredGrid -> Bool type GridCache = S.Set Grid @@ -42,13 +44,13 @@ validCoord :: Coord -> Bool validCoord (r, c) = r >= 1 && r <= gridSize && c >= 1 && c <= gridSize -instance Distributive Matrix where +instance Distributive X.Matrix where distribute = distributeRep -instance Representable Matrix where - type Rep Matrix = Coord - index m c = m ! c -- mGet c m - tabulate = matrix gridSize gridSize +instance Representable X.Matrix where + type Rep X.Matrix = Coord + index m c = (X.!) m c -- mGet c m + tabulate = X.matrix gridSize gridSize gridSize :: Int gridSize = 5 @@ -73,7 +75,7 @@ step = extend render :: StoredGrid -> String -- render (StoreT (Identity g) _) = foldMap ((++ "\n") . foldMap (bool "." "#")) g -render grid = prettyMatrix $ mapPos (\_ c -> bool "." "#" c) g +render grid = X.prettyMatrix $ X.mapPos (\_ c -> bool "." "#" c) g where g = unGrid grid @@ -82,7 +84,7 @@ mkGrid xs = store (`elem` xs) (1, 1) unGrid :: StoredGrid -> Grid -- unGrid (StoreT (Identity g) _) = g -unGrid grid = fromList gridSize gridSize gridList +unGrid grid = X.fromList gridSize gridSize gridList where (sgf, _sgl) = runStore grid gridList = [sgf (r, c) | r <- [1..gridSize], c <- [1..gridSize]] @@ -147,4 +149,4 @@ fGridCache gs = scanl' (flip S.insert) S.empty gs bioDiversity :: Grid -> Integer bioDiversity g = sum $ map snd $ filter (id . fst) $ zip bugs $ iterate ( * 2) 1 - where bugs = toList g + where bugs = X.toList g