2015-03-08 01:35:22 +01:00
|
|
|
import Control.Applicative
|
2015-03-08 01:49:19 +01:00
|
|
|
import Data.List ((\\))
|
|
|
|
import Data.Maybe (fromMaybe)
|
2014-12-19 20:09:30 +01:00
|
|
|
import Matrix
|
2014-12-19 20:02:47 +01:00
|
|
|
|
|
|
|
type Cell = Int
|
2014-12-19 20:09:30 +01:00
|
|
|
type Grid = Mat Cell
|
2014-12-19 20:02:47 +01:00
|
|
|
|
2014-12-19 20:24:16 +01:00
|
|
|
-- | Print a grid
|
|
|
|
gprint :: Grid -> IO ()
|
|
|
|
gprint = putStrLn . map replace . show
|
|
|
|
where
|
|
|
|
replace '1' = '■'
|
|
|
|
replace '0' = '.'
|
|
|
|
replace a = a
|
2014-12-19 20:02:47 +01:00
|
|
|
|
|
|
|
-- | Get the state of a cell
|
|
|
|
-- 0 when out of the grid
|
|
|
|
(!) :: Grid -> Pos -> Cell
|
2015-03-08 01:49:19 +01:00
|
|
|
(Mat g) ! (x, y) = fromMaybe 0 (g ?? y >>= (?? x))
|
2014-12-19 20:02:47 +01:00
|
|
|
|
2014-12-19 20:09:30 +01:00
|
|
|
-- | List of neighbours cells
|
2014-12-19 20:02:47 +01:00
|
|
|
near :: Grid -> Pos -> [Cell]
|
2015-03-08 01:35:22 +01:00
|
|
|
near g (x, y) = map state $ neighbours \\ [(0,0)]
|
|
|
|
where
|
|
|
|
neighbours = liftA2 (,) [-1..1] [-1..1]
|
|
|
|
state (x', y') = g ! (x+x', y+y')
|
2014-12-19 20:02:47 +01:00
|
|
|
|
|
|
|
-- | Find if a cell will be alive in the next generation
|
|
|
|
alive :: Grid -> Pos -> Cell
|
|
|
|
alive g p
|
|
|
|
| v == 0 && n == 3 = 1
|
|
|
|
| v == 1 && (n == 2 || n == 3) = 1
|
|
|
|
| otherwise = 0
|
2015-03-08 01:35:22 +01:00
|
|
|
where (n, v) = (sum (near g p), g ! p)
|
2014-12-19 20:02:47 +01:00
|
|
|
|
|
|
|
-- | Compute next generation
|
|
|
|
next :: Grid -> Grid
|
2015-03-08 01:35:22 +01:00
|
|
|
next g = alive g <$> indeces g
|
2014-12-19 20:02:47 +01:00
|
|
|
|
|
|
|
main :: IO ()
|
2014-12-19 20:24:16 +01:00
|
|
|
main = mapM_ gprint (iterate next grid)
|
2014-12-19 20:02:47 +01:00
|
|
|
|
2015-03-08 01:49:19 +01:00
|
|
|
grid = Mat
|
|
|
|
[ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
2014-12-19 20:02:47 +01:00
|
|
|
, [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
|
|
|
, [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
|
|
|
|
, [0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]
|
|
|
|
, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
|
|
|
, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
2015-03-08 01:49:19 +01:00
|
|
|
, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ]
|