projects
/
advent-of-code-19.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Trying to get part 1 working with a Map, not a Matrix
[advent-of-code-19.git]
/
advent24
/
src
/
advent24.hs
diff --git
a/advent24/src/advent24.hs
b/advent24/src/advent24.hs
index e784cdc05665c0ea50c6acb7aa5519703c8173b5..34cf1138bf1574b60b7bf5f2b3980dc2658361b8 100644
(file)
--- 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.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.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 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
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 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
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
validCoord (r, c) = r >= 1 && r <= gridSize && c >= 1 && c <= gridSize
-instance Distributive Matrix where
+instance Distributive
X.
Matrix where
distribute = distributeRep
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
gridSize :: Int
gridSize = 5
@@
-73,7
+75,7
@@
step = extend
render :: StoredGrid -> String
-- render (StoreT (Identity g) _) = foldMap ((++ "\n") . foldMap (bool "." "#")) g
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
where g = unGrid grid
@@
-82,7
+84,7
@@
mkGrid xs = store (`elem` xs) (1, 1)
unGrid :: StoredGrid -> Grid
-- unGrid (StoreT (Identity g) _) = g
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]]
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
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