Skip to content

Instantly share code, notes, and snippets.

@bjartek
Created March 31, 2011 12:32
Show Gist options
  • Save bjartek/896275 to your computer and use it in GitHub Desktop.
Save bjartek/896275 to your computer and use it in GitHub Desktop.
val input = "..*\n...\n*..";
val grid = input.split("\n").map(_.toArray)
def bomb(coord: Tuple2[Int,Int]) = {
for{
row <- grid.lift(coord._1)
cell <- row.lift(coord._2)
if(cell == '*')
} yield 1
}
def nearby(x:Int, y:Int) = for( row <- x-1 to x+1; col <- y-1 to y+1) yield (row,col)
def score(x:Int, y:Int) : String = bomb2(x,y) match {
case Some(x) => " "
case None => nearby(x,y).flatMap(bomb2).sum.toString
}
var cells = grid.indices.map( x => grid(x).indices.map( y => score(x,y)))
cells.foreach(x => println(x.mkString))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment