Skip to content

Instantly share code, notes, and snippets.

@bavardage
Created January 8, 2011 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bavardage/770395 to your computer and use it in GitHub Desktop.
Save bavardage/770395 to your computer and use it in GitHub Desktop.
*Main Data.Bits> printRuleLines 20 90 [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
#
# #
# #
# # # #
# #
# # # #
# # # #
# # # # # # # #
# #
# # # #
# # # #
# # # # # # # #
# # # #
# # # # # # # #
# # # # # # # #
# # # # # # # # # # # # # # # #
# #
# # # #
# # # #
# # # # # # # #
import Data.Bits
threes :: [a] -> [(a,a,a)]
threes (x:y:z:xs) = (x,y,z) : threes (y:z:xs)
threes (x:y:[]) = []
firstThree (x:y:xs) = (last xs,x,y)
lastThree (x:xs) = last $ threes (xs ++ [x])
neighbourStates :: [a] -> [(a,a,a)]
neighbourStates xs = firstThree xs : threes xs ++ [lastThree xs]
transition :: Int -> (Int,Int,Int) -> Int
transition rule (l,c,r) = if (2^(l*4 + c*2 + r) .&. rule) == 0
then 0
else 1
step rule xs = map (transition rule) $ neighbourStates xs
showState (1:xs) = '#' : showState xs
showState (0:xs) = ' ' : showState xs
showState [] = ""
printRuleLines n r xs = mapM_ putStrLn $ map showState $ take n $ iterate (step r) xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment