Skip to content

Instantly share code, notes, and snippets.

@ahoy-jon
Created January 5, 2013 13:56
Show Gist options
  • Save ahoy-jon/4461666 to your computer and use it in GitHub Desktop.
Save ahoy-jon/4461666 to your computer and use it in GitHub Desktop.
object GOL extends App {
type Cell = (Int,Int)
type Step = List[Cell]
lazy val motifvoisinage:Step = {
val iii = (-1 to 1)
iii.flatMap(x => iii.map( _ -> x)).filter(_ != (0,0)).toList
}
def add(c1:Cell, c2:Cell):Cell = (c1._1 + c2._1, c2._2 + c1._2)
def voisinage(s:Step) = for(i <- s; j <- motifvoisinage) yield (add(i,j))
def nextStep(s:Step):Step = {
voisinage(s).groupBy(x=> x).mapValues(_.size).filter(t => {
val (k,v) = t
(v == 3) ||
(v == 2 && s.contains(k))}).keys.toList
}
def printStep(s:Step) {
def rangeFromList(li:List[Int]) = {li.min to li.max}
for ( x <- rangeFromList(s.map(_._1))) {
println((for (y <- rangeFromList(s.map(_._2))) yield {
if(s.contains((x,y))) "X" else " "}).mkString)
}
}
var mystep = glider
(1 to 100).foreach ( i => {
printStep(mystep)
println("-----------")
mystep = nextStep(mystep)
})
lazy val glider = blinker ::: List((1,3), (2,2)) ::: List((-1,-1),(-2,-2), (-1,-2),(-2,-1))
lazy val blinker = List((0,1),(0,2),(0,3))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment