-- 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)
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
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
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
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]]
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