Skip to content

Instantly share code, notes, and snippets.

@kamiyaowl
Last active August 29, 2015 14:15
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 kamiyaowl/34ac191e5ecf53bd9b8a to your computer and use it in GitHub Desktop.
Save kamiyaowl/34ac191e5ecf53bd9b8a to your computer and use it in GitHub Desktop.
def Y[A,B]( f:((A => B), A ) => B, x:A ):B = f( (y:A) => Y(f,y),x)
((size:(Int,Int)) => Y(
(f:Map[(Int,Int),Boolean] => Stream[Map[(Int,Int),Boolean]], seed:Map[(Int,Int),Boolean]) =>
((curr : Map[(Int,Int),Boolean]) => Stream.cons(curr,f(curr))) {
( for (x <- 0 to size._1; y <- 0 to size._2) yield List(
seed.get(x - 1, y - 1), seed.get(x, y - 1), seed.get(x + 1, y - 1), seed.get(x - 1, y), seed.get(x + 1, y), seed.get(x - 1, y + 1), seed.get(x, y + 1), seed.get(x + 1, y + 1))
count { case Some(z) => z; case None => false }
match { case z if !seed(x, y) && z == 3 => (x, y) -> true ; case z if seed(x, y) && (z == 2 || z == 3) => (x, y) -> true ; case _ => (x, y) -> false }
).toMap },
((s:(Int,Int)) => { for(x <- 0 to size._1 ; y <- 0 to size._2) yield (x,y) -> util.Random.nextBoolean }.toMap)(size)
).map { _.toIndexedSeq.sortBy{ case ((x,y),_) => x + y * size._1 }.map{ case ((_,_),b) => if(b) "#" else " " }.grouped(size._1).map(_.mkString("","","\n")).mkString("=" * size._1 + "\n","","") }.foreach(x => { readLine(); println(x) })
)(30,10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment