module Nove.Cells where
import Zero ( prev, next, (∈), count )
import Nove ( Cell )
import Data.Map as Map ( size, filter )
-- Automaton terra :: Automaton
terra :: (Eq a, Enum a, Ord a, Bounded a) => Int -> Cell state a
terra x state ns a
| leq > x = a
| lgt < llt = prev a
| otherwise = next a
where
leq, lgt, llt :: Int
leq = count (== a) ns
lgt = count (> a) ns
llt = count (< a) ns
waves :: (Eq a, Enum a, Bounded a) => Cell state a
waves state ns a
| a == minBound , maxBound ∈ ns = maxBound
| otherwise = prev a
pulse :: (Enum a, Bounded a, Ord a) => Cell state a
pulse state ns a = prev $ maximum ns
dois :: (Eq a, Enum a, Bounded a, Ord a, Num a) => Cell state a
dois state ns a
| count (== minimum ns) ns <= fromEnum a - fromEnum (minimum ns) = prev a
| fromEnum (sum $ Map.filter (> a) ns) > Map.size ns = next a
| otherwise = a
bees :: (Eq a, Enum a, Bounded a, Ord a) => Cell state a
bees state ns a
| count (> a) ns ∈ [2 :: Int] = next a
| count (< a) ns ∈ [2 :: Int] = a
| otherwise = prev a
gene :: (Enum a, Bounded a, Ord a) => [Int] -> [Int] -> Int -> Cell state a
gene survive born n state ns a
| count (== maxBound) ns ∈ survive , fromEnum a > n - 2 = maxBound
| count (== maxBound) ns ∈ born , a < succ minBound = maxBound
| fromEnum a <= n - 2 = prev a
| otherwise = toEnum $ n - 2