From: Neil Smith Date: Wed, 12 Dec 2018 22:38:22 +0000 (+0000) Subject: Day 12 X-Git-Url: https://git.njae.me.uk/?p=advent-of-code-18.git;a=commitdiff_plain;h=bb244d2255e1a7e93450df36cfe0145728b50b3d Day 12 --- diff --git a/advent-of-code.cabal b/advent-of-code.cabal index 2910874..517763e 100644 --- a/advent-of-code.cabal +++ b/advent-of-code.cabal @@ -159,3 +159,19 @@ executable advent11naive default-language: Haskell2010 build-depends: base >= 4.7 && < 5 , containers + +executable advent12 + hs-source-dirs: src/advent12 + main-is: advent12.hs + default-language: Haskell2010 + build-depends: base >= 4.7 && < 5 + , text + , megaparsec + , containers + +executable life + hs-source-dirs: src/advent12 + main-is: life.hs + default-language: Haskell2010 + build-depends: base >= 4.7 && < 5 + , comonad diff --git a/data/advent12-small.txt b/data/advent12-small.txt new file mode 100644 index 0000000..a2a74be --- /dev/null +++ b/data/advent12-small.txt @@ -0,0 +1,34 @@ +initial state: #..#.#..##......###...### + +##### => . +####. => # +###.# => # +###.. => # +##.## => # +##.#. => # +##..# => . +##... => . +#.### => # +#.##. => . +#.#.# => # +#.#.. => . +#..## => . +#..#. => . +#...# => . +#.... => . +.#### => # +.###. => . +.##.# => . +.##.. => # +.#.## => # +.#.#. => # +.#..# => . +.#... => # +..### => . +..##. => . +..#.# => . +..#.. => # +...## => # +...#. => . +....# => . +..... => . diff --git a/data/advent12.txt b/data/advent12.txt new file mode 100644 index 0000000..65bf60b --- /dev/null +++ b/data/advent12.txt @@ -0,0 +1,34 @@ +initial state: #.##.###.#.##...##..#..##....#.#.#.#.##....##..#..####..###.####.##.#..#...#..######.#.....#..##...# + +.#.#. => . +...#. => # +..##. => . +....# => . +##.#. => # +.##.# => # +.#### => # +#.#.# => # +#..#. => # +##..# => . +##### => . +...## => . +.#... => . +###.. => # +#..## => . +#...# => . +.#..# => # +.#.## => . +#.#.. => # +..... => . +####. => . +##.## => # +..### => # +#.... => . +###.# => . +.##.. => # +#.### => # +..#.# => . +.###. => # +##... => # +#.##. => # +..#.. => # diff --git a/problems/day12.html b/problems/day12.html new file mode 100644 index 0000000..631922e --- /dev/null +++ b/problems/day12.html @@ -0,0 +1,178 @@ + + + + +Day 12 - Advent of Code 2018 + + + + + + + +

Advent of Code

Neil Smith (AoC++) 24*

 {'year':2018}

+ + + +
+

--- Day 12: Subterranean Sustainability ---

The year 518 is significantly more underground than your history books implied. Either that, or you've arrived in a vast cavern network under the North Pole.

+

After exploring a little, you discover a long tunnel that contains a row of small pots as far as you can see to your left and right. A few of them contain plants - someone is trying to grow things in these geothermally-heated caves.

+

The pots are numbered, with 0 in front of you. To the left, the pots are numbered -1, -2, -3, and so on; to the right, 1, 2, 3.... Your puzzle input contains a list of pots from 0 to the right and whether they do (#) or do not (.) currently contain a plant, the initial state. (No other pots currently contain plants.) For example, an initial state of #..##.... indicates that pots 0, 3, and 4 currently contain plants.

+

Your puzzle input also contains some notes you find on a nearby table: someone has been trying to figure out how these plants spread to nearby pots. Based on the notes, for each generation of plants, a given pot has or does not have a plant based on whether that pot (and the two pots on either side of it) had a plant in the last generation. These are written as LLCRR => N, where L are pots to the left, C is the current pot being considered, R are the pots to the right, and N is whether the current pot will have a plant in the next generation. For example:

+
    +
  • A note like ..#.. => . means that a pot that contains a plant but with no plants within two pots of it will not have a plant in it during the next generation.
  • +
  • A note like ##.## => . means that an empty pot with two plants on each side of it will remain empty in the next generation.
  • +
  • A note like .##.# => # means that a pot has a plant in a given generation if, in the previous generation, there were plants in that pot, the one immediately to the left, and the one two pots to the right, but not in the ones immediately to the right and two to the left.
  • +
+

It's not clear what these plants are for, but you're sure it's important, so you'd like to make sure the current configuration of plants is sustainable by determining what will happen after 20 generations.

+

For example, given the following input:

+
initial state: #..#.#..##......###...###
+
+...## => #
+..#.. => #
+.#... => #
+.#.#. => #
+.#.## => #
+.##.. => #
+.#### => #
+#.#.# => #
+#.### => #
+##.#. => #
+##.## => #
+###.. => #
+###.# => #
+####. => #
+
+

For brevity, in this example, only the combinations which do produce a plant are listed. (Your input includes all possible combinations.) Then, the next 20 generations will look like this:

+
                 1         2         3     
+       0         0         0         0     
+ 0: ...#..#.#..##......###...###...........
+ 1: ...#...#....#.....#..#..#..#...........
+ 2: ...##..##...##....#..#..#..##..........
+ 3: ..#.#...#..#.#....#..#..#...#..........
+ 4: ...#.#..#...#.#...#..#..##..##.........
+ 5: ....#...##...#.#..#..#...#...#.........
+ 6: ....##.#.#....#...#..##..##..##........
+ 7: ...#..###.#...##..#...#...#...#........
+ 8: ...#....##.#.#.#..##..##..##..##.......
+ 9: ...##..#..#####....#...#...#...#.......
+10: ..#.#..#...#.##....##..##..##..##......
+11: ...#...##...#.#...#.#...#...#...#......
+12: ...##.#.#....#.#...#.#..##..##..##.....
+13: ..#..###.#....#.#...#....#...#...#.....
+14: ..#....##.#....#.#..##...##..##..##....
+15: ..##..#..#.#....#....#..#.#...#...#....
+16: .#.#..#...#.#...##...#...#.#..##..##...
+17: ..#...##...#.#.#.#...##...#....#...#...
+18: ..##.#.#....#####.#.#.#...##...##..##..
+19: .#..###.#..#.#.#######.#.#.#..#.#...#..
+20: .#....##....#####...#######....#.#..##.
+
+

The generation is shown along the left, where 0 is the initial state. The pot numbers are shown along the top, where 0 labels the center pot, negative-numbered pots extend to the left, and positive pots extend toward the right. Remember, the initial state begins at pot 0, which is not the leftmost pot used in this example.

+

After one generation, only seven plants remain. The one in pot 0 matched the rule looking for ..#.., the one in pot 4 matched the rule looking for .#.#., pot 9 matched .##.., and so on.

+

In this example, after 20 generations, the pots shown as # contain plants, the furthest left of which is pot -2, and the furthest right of which is pot 34. Adding up all the numbers of plant-containing pots after the 20th generation produces 325.

+

After 20 generations, what is the sum of the numbers of all pots which contain a plant?

+
+

Your puzzle answer was 2571.

--- Part Two ---

You realize that 20 generations aren't enough. After all, these plants will need to last another 1500 years to even reach your timeline, not to mention your future.

+

After fifty billion (50000000000) generations, what is the sum of the numbers of all pots which contain a plant?

+
+

Your puzzle answer was 3100000000655.

Both parts of this puzzle are complete! They provide two gold stars: **

+

At this point, you should return to your advent calendar and try another puzzle.

+

If you still want to see it, you can get your puzzle input.

+

You can also this puzzle.

+
+ + + + + + \ No newline at end of file diff --git a/src/advent12/advent12-comonad.hs b/src/advent12/advent12-comonad.hs new file mode 100644 index 0000000..9e46240 --- /dev/null +++ b/src/advent12/advent12-comonad.hs @@ -0,0 +1,220 @@ +{-# LANGUAGE OverloadedStrings, DeriveFunctor #-} + +-- import Data.List + +import Data.Text (Text) +import qualified Data.Text.IO as TIO + +import Data.Void (Void) + +import Text.Megaparsec +import Text.Megaparsec.Char +import qualified Text.Megaparsec.Char.Lexer as L +import qualified Control.Applicative as CA + +import Control.Applicative +import Control.Comonad + +import Control.Monad (forM_) + +-- import qualified Data.Set as S + + +-- main :: IO () +-- main = do +-- text <- TIO.readFile "data/advent12.txt" +-- let (initial, rules) = successfulParse text +-- print initial +-- print rules + + +main :: IO () +main = do + let ig = tape X initialGame + let tl = timeline conwayRules ig + forM_ (takeS 5 tl) $ \s -> do + printSheet 3 s + putStrLn "---" + + +-- These two typeclasses probably make some people groan. +class LeftRight t where + left :: t a -> t a + right :: t a -> t a + +-- | An infinite list of values +data Stream a = a :> Stream a + deriving (Functor) + + +-- * Stream utilities + +-- | Allows finitary beings to view slices of a 'Stream' +takeS :: Int -> Stream a -> [a] +takeS 0 _ = [] +takeS n (x :> xs) = x : (takeS (n-1) xs) + +-- | Build a 'Stream' from a generator function +unfoldS :: (b -> (a, b)) -> b -> Stream a +unfoldS f c = x :> unfoldS f d + where (x, d) = f c + +-- | Build a 'Stream' by mindlessly repeating a value +repeatS :: a -> Stream a +repeatS x = x :> repeatS x + +-- | Build a 'Stream' from a finite list, filling in the rest with a designated +-- default value +fromS :: a -> [a] -> Stream a +fromS def [] = repeatS def +fromS def (x:xs) = x :> (fromS def xs) + +-- | Prepend values from a list to an existing 'Stream' +prependList :: [a] -> Stream a -> Stream a +prependList [] str = str +prependList (x:xs) str = x :> (prependList xs str) + +-- | A 'Stream' is a comonad +instance Comonad Stream where + extract (a :> _) = a + duplicate s@(a :> as) = s :> (duplicate as) + +-- | It is also a 'ComonadApply' +instance ComonadApply Stream where + (f :> fs) <@> (x :> xs) = (f x) :> (fs <@> xs) + +-- * Tapes, our workhorse + +-- | A 'Tape' is always focused on one value in a 1-dimensional infinite stream +data Tape a = Tape (Stream a) a (Stream a) + deriving (Functor) + +-- | We can go left and right along a tape! +instance LeftRight Tape where + left (Tape (l :> ls) c rs) = Tape ls l (c :> rs) + right (Tape ls c (r :> rs)) = Tape (c :> ls) r rs + +-- | Build out the left, middle, and right parts of a 'Tape' with generator +-- functions. +unfoldT + :: (c -> (a, c)) + -> (c -> a) + -> (c -> (a, c)) + -> c + -> Tape a +unfoldT prev center next = + Tape + <$> unfoldS prev + <*> center + <*> unfoldS next + +-- | A simplified unfolding mechanism that will be useful shortly +tapeIterate + :: (a -> a) + -> (a -> a) + -> a + -> Tape a +tapeIterate prev next = unfoldT (dup . prev) id (dup . next) + where dup a = (a, a) + +-- | Create a 'Tape' from a list where everything to the left is some default +-- value and the list is the focused value and everything to the right. +tapeFromList :: a -> [a] -> Tape a +tapeFromList def xs = right $ Tape background def $ fromS def xs + where background = repeatS def + +tapeToList :: Int -> Tape a -> [a] +tapeToList n (Tape ls x rs) = + reverse (takeS n ls) ++ [x] ++ (takeS n rs) + +instance Comonad Tape where + extract (Tape _ c _) = c + duplicate = tapeIterate left right + +instance ComonadApply Tape where + (Tape fl fc fr) <@> (Tape xl xc xr) = + Tape (fl <@> xl) (fc xc) (fr <@> xr) + + +-- | Construct an infinite tape in all directions from a base value. +background :: a -> Tape a +background x = Tape (repeatS x) x (repeatS x) + +-- | This just makes some things more readable +(&) :: a -> (a -> b) -> b +(&) = flip ($) + +-- | A cell can be alive ('O') or dead ('X'). +data Cell = X | O deriving (Eq) + +instance Show Cell where + show X = "\x2591" + show O = "\x2593" + +-- | The first list is of numbers of live neighbors to make a dead 'Cell' become +-- alive; the second is numbers of live neighbors that keeps a live 'Cell' +-- alive. +type Ruleset = ([Int], [Int]) + +cellToInt :: Cell -> Int +cellToInt X = 0 +cellToInt O = 1 + +conwayRules :: Ruleset +conwayRules = ([3], [2, 3]) + +executeRules :: Ruleset -> Tape Cell -> Cell +executeRules rules s = go (extract s) where + + -- there is probably a more elegant way of getting all 8 neighbors + neighbors = [ s & left & left & extract + , s & left & extract + , s & extract + , s & right & extract + , s & right & right & extract + ] + liveCount = sum $ map cellToInt neighbors + go O | liveCount `elem` persist = O + | otherwise = X + go X | liveCount `elem` born = O + | otherwise = X + +-- | A simple glider +initialGame :: [Cell] +initialGame = map cToCell "#..#.#..##......###...###" + where cToCell c = if c == '#' then O else X + +-- | A 'Stream' of Conway games. +timeline :: Ruleset -> Tape Cell -> Stream (Tape Cell) +timeline rules ig = go ig where + go ig = ig :> (go (rules' <@> (duplicate ig))) + rules' = background $ executeRules rules + + +applyRule rule cells = + + +-- Parse the input file + +type Parser = Parsec Void Text + +sc :: Parser () +sc = L.space (skipSome spaceChar) CA.empty CA.empty + +symb = L.symbol sc +potP = char '.' <|> char '#' + +initialPrefix = symb "initial state:" +ruleSepP = symb "=>" + +fileP = (,) <$> initialP <*> many ruleP +initialP = initialPrefix *> many potP <* sc +ruleP = (,) <$> ruleLHS <* ruleSepP <*> ruleRHS +ruleLHS = count 5 potP <* sc +ruleRHS = potP <* sc + +successfulParse :: Text -> (String, [(String, Char)]) +successfulParse input = + case parse fileP "input" input of + Left _error -> ("", []) -- TIO.putStr $ T.pack $ parseErrorPretty err + Right world -> world \ No newline at end of file diff --git a/src/advent12/advent12.hs b/src/advent12/advent12.hs new file mode 100644 index 0000000..cbf75db --- /dev/null +++ b/src/advent12/advent12.hs @@ -0,0 +1,89 @@ +{-# LANGUAGE OverloadedStrings #-} + +import Data.Text (Text) +import qualified Data.Text.IO as TIO + +import Data.Void (Void) + +import Text.Megaparsec +import Text.Megaparsec.Char +import qualified Text.Megaparsec.Char.Lexer as L +import qualified Control.Applicative as CA + +import Data.List +import qualified Data.Set as S + +type Pots = S.Set Int +data Rule = Rule [Bool] Bool deriving (Eq, Show) + +main :: IO () +main = do + text <- TIO.readFile "data/advent12.txt" + let (initial, rules) = successfulParse text + let row = makeWorld 0 initial + print $ part1 rules row + print $ part2 rules row + +part1 :: [Rule] -> Pots -> Int +part1 rules row = sum $ (iterate (\r -> applyRules rules r) row)!!20 + + +part2 :: [Rule] -> Pots -> Integer +-- part2 rules pots = (length differentQuads, steadyDiff, sum la, sum lb, sum lc, sum ld)-- (fromIntegral (sum la)) + steadyDiff * remainingGenerations +part2 rules pots = (fromIntegral (sum lc)) + steadyDiff * remainingGenerations + where rows = (iterate (\r -> applyRules rules r) pots) + rowQuads = zip4 rows (drop 1 rows) (drop 2 rows) (drop 3 rows) + sameDiffs (a, b, c, d) = length (nub [(sum a) - (sum b), (sum b) - (sum c), (sum c) - (sum d) ]) == 1 + differentQuads = takeWhile (not . sameDiffs) rowQuads + (_la, _lb, lc, ld) = last differentQuads + remainingGenerations = 50000000000 - (fromIntegral (length differentQuads)) - 1 + steadyDiff = fromIntegral $ (sum ld) - (sum lc) + + +makeWorld :: Int -> [Bool] -> Pots +makeWorld start = S.fromList . map fst . filter snd . zip [start..] + +applyRuleAt :: [Rule] -> Int -> Pots -> (Int, Bool) +applyRuleAt rules location pots = (location, result) + where (Rule _ result) = head $ filter (\r -> matchRuleAt r location pots) rules + +matchRuleAt :: Rule -> Int -> Pots -> Bool +matchRuleAt (Rule pattern _) location pots = patternHere == potsHere + where patternHere = makeWorld (location - 2) pattern + potsHere = S.filter (\l -> abs (location - l) <= 2) pots + + +applyRules :: [Rule] -> Pots -> Pots +applyRules rules pots = S.fromList $ map fst $ filter snd potValues + where start = S.findMin pots + end = S.findMax pots + potValues = map (\location -> applyRuleAt rules location pots) [(start-3)..(end+3)] + +-- showPots pots = map (\i -> showPot i pots) [-10..110] +-- where showPot i pots = if i `S.member` pots then '#' else '.' + + +-- Parse the input file + +type Parser = Parsec Void Text + +sc :: Parser () +sc = L.space (skipSome spaceChar) CA.empty CA.empty + +symb = L.symbol sc +potP = (char '.' *> pure False) <|> (char '#' *> pure True) + +initialPrefix = symb "initial state:" +ruleSepP = symb "=>" + +fileP = (,) <$> initialP <*> many ruleP +initialP = initialPrefix *> many potP <* sc +ruleP = Rule <$> ruleLHS <* ruleSepP <*> ruleRHS +ruleLHS = count 5 potP <* sc +ruleRHS = potP <* sc + +successfulParse :: Text -> ([Bool], [Rule]) +successfulParse input = + case parse fileP "input" input of + Left _error -> ([], []) -- TIO.putStr $ T.pack $ parseErrorPretty err + Right world -> world \ No newline at end of file diff --git a/src/advent12/life.hs b/src/advent12/life.hs new file mode 100644 index 0000000..840ac8c --- /dev/null +++ b/src/advent12/life.hs @@ -0,0 +1,218 @@ +{-# LANGUAGE DeriveFunctor #-} + +import Control.Applicative +import Control.Comonad + +import Control.Monad (forM_) + +-- These two typeclasses probably make some people groan. +class LeftRight t where + left :: t a -> t a + right :: t a -> t a + +class UpDown t where + up :: t a -> t a + down :: t a -> t a + +-- | An infinite list of values +data Stream a = a :> Stream a + deriving (Functor) + + +-- * Stream utilities + +-- | Allows finitary beings to view slices of a 'Stream' +takeS :: Int -> Stream a -> [a] +takeS 0 _ = [] +takeS n (x :> xs) = x : (takeS (n-1) xs) + +-- | Build a 'Stream' from a generator function +unfoldS :: (b -> (a, b)) -> b -> Stream a +unfoldS f c = x :> unfoldS f d + where (x, d) = f c + +-- | Build a 'Stream' by mindlessly repeating a value +repeatS :: a -> Stream a +repeatS x = x :> repeatS x + +-- | Build a 'Stream' from a finite list, filling in the rest with a designated +-- default value +fromS :: a -> [a] -> Stream a +fromS def [] = repeatS def +fromS def (x:xs) = x :> (fromS def xs) + +-- | Prepend values from a list to an existing 'Stream' +prependList :: [a] -> Stream a -> Stream a +prependList [] str = str +prependList (x:xs) str = x :> (prependList xs str) + +-- | A 'Stream' is a comonad +instance Comonad Stream where + extract (a :> _) = a + duplicate s@(a :> as) = s :> (duplicate as) + +-- | It is also a 'ComonadApply' +instance ComonadApply Stream where + (f :> fs) <@> (x :> xs) = (f x) :> (fs <@> xs) + +-- * Tapes, our workhorse + +-- | A 'Tape' is always focused on one value in a 1-dimensional infinite stream +data Tape a = Tape (Stream a) a (Stream a) + deriving (Functor) + +-- | We can go left and right along a tape! +instance LeftRight Tape where + left (Tape (l :> ls) c rs) = Tape ls l (c :> rs) + right (Tape ls c (r :> rs)) = Tape (c :> ls) r rs + +-- | Build out the left, middle, and right parts of a 'Tape' with generator +-- functions. +unfoldT + :: (c -> (a, c)) + -> (c -> a) + -> (c -> (a, c)) + -> c + -> Tape a +unfoldT prev center next = + Tape + <$> unfoldS prev + <*> center + <*> unfoldS next + +-- | A simplified unfolding mechanism that will be useful shortly +tapeIterate + :: (a -> a) + -> (a -> a) + -> a + -> Tape a +tapeIterate prev next = unfoldT (dup . prev) id (dup . next) + where dup a = (a, a) + +-- | Create a 'Tape' from a list where everything to the left is some default +-- value and the list is the focused value and everything to the right. +tapeFromList :: a -> [a] -> Tape a +tapeFromList def xs = right $ Tape background def $ fromS def xs + where background = repeatS def + +tapeToList :: Int -> Tape a -> [a] +tapeToList n (Tape ls x rs) = + reverse (takeS n ls) ++ [x] ++ (takeS n rs) + +instance Comonad Tape where + extract (Tape _ c _) = c + duplicate = tapeIterate left right + +instance ComonadApply Tape where + (Tape fl fc fr) <@> (Tape xl xc xr) = + Tape (fl <@> xl) (fc xc) (fr <@> xr) + +-- * Sheets! We're so close! + +-- | A 2-dimensional 'Tape' of 'Tape's. +newtype Sheet a = Sheet (Tape (Tape a)) + deriving (Functor) + +instance UpDown Sheet where + up (Sheet p) = Sheet (left p) + down (Sheet p) = Sheet (right p) + +instance LeftRight Sheet where + left (Sheet p) = Sheet (fmap left p) + right (Sheet p) = Sheet (fmap right p) + +-- Helper functions to take a given 'Sheet' and make an infinite 'Tape' of it. +horizontal :: Sheet a -> Tape (Sheet a) +horizontal = tapeIterate left right + +vertical :: Sheet a -> Tape (Sheet a) +vertical = tapeIterate up down + +instance Comonad Sheet where + extract (Sheet p) = extract $ extract p + -- | See? Told you 'tapeIterate' would be useful + duplicate s = Sheet $ fmap horizontal $ vertical s + +instance ComonadApply Sheet where + (Sheet (Tape fu fc fd)) <@> (Sheet (Tape xu xc xd)) = + Sheet $ Tape (fu `ap` xu) (fc <@> xc) (fd `ap` xd) + + where ap t1 t2 = (fmap (<@>) t1) <@> t2 + +-- | Produce a 'Sheet' from a list of lists, where each inner list is a row. +sheet :: a -> [[a]] -> Sheet a +sheet def grid = Sheet cols + where cols = fmap (tapeFromList def) rows + rows = tapeFromList [def] grid + +-- | Slice a sheet for viewing purposes. +sheetToList :: Int -> Sheet a -> [[a]] +sheetToList n (Sheet zs) = tapeToList n $ fmap (tapeToList n) zs + +-- | Construct an infinite sheet in all directions from a base value. +background :: a -> Sheet a +background x = Sheet . duplicate $ Tape (repeatS x) x (repeatS x) + +printSheet :: Show a => Int -> Sheet a -> IO () +printSheet n sh = forM_ (sheetToList n sh) $ putStrLn . show + +-- | This just makes some things more readable +(&) :: a -> (a -> b) -> b +(&) = flip ($) + +-- | A cell can be alive ('O') or dead ('X'). +data Cell = X | O deriving (Eq) + +instance Show Cell where + show X = "\x2591" + show O = "\x2593" + +-- | The first list is of numbers of live neighbors to make a dead 'Cell' become +-- alive; the second is numbers of live neighbors that keeps a live 'Cell' +-- alive. +type Ruleset = ([Int], [Int]) + +cellToInt :: Cell -> Int +cellToInt X = 0 +cellToInt O = 1 + +conwayRules :: Ruleset +conwayRules = ([3], [2, 3]) + +executeRules :: Ruleset -> Sheet Cell -> Cell +executeRules (born, persist) s = go (extract s) where + + -- there is probably a more elegant way of getting all 8 neighbors + neighbors = [ s & up & extract + , s & down & extract + , s & left & extract + , s & right & extract + , s & up & right & extract + , s & up & left & extract + , s & down & left & extract + , s & down & right & extract ] + liveCount = sum $ map cellToInt neighbors + go O | liveCount `elem` persist = O + | otherwise = X + go X | liveCount `elem` born = O + | otherwise = X + +-- | A simple glider +initialGame :: [[Cell]] +initialGame = [ [ X, X, O ] + , [ O, X, O ] + , [ X, O, O ] ] + +-- | A 'Stream' of Conway games. +timeline :: Ruleset -> Sheet Cell -> Stream (Sheet Cell) +timeline rules ig = go ig where + go ig = ig :> (go (rules' <@> (duplicate ig))) + rules' = background $ executeRules rules + +main :: IO () +main = do + let ig = sheet X initialGame + let tl = timeline conwayRules ig + forM_ (takeS 5 tl) $ \s -> do + printSheet 3 s + putStrLn "---"