Skip to content

Instantly share code, notes, and snippets.

@justinhj
Created September 17, 2020 00:41
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 justinhj/9ece99e607a6e9403e53ba697ae4726e to your computer and use it in GitHub Desktop.
Save justinhj/9ece99e607a6e9403e53ba697ae4726e to your computer and use it in GitHub Desktop.
def play(gridStr: String): String = {
val grid = convertStringToGrid(gridStr)
val newG = grid.zipWithIndex.map {
case (row, x) =>
row.zipWithIndex.map {
case (col, y) => {
val count = countNeighbours(grid, x, y)
if(count == 2 || count == 3)
true
else if (grid(x)(y) == false && count == 3)
true
else false
}
}
}
convertGridToString(newG)
}
def countNeighbours(grid : Vector[Vector[Boolean]], x: Int, y: Int): Int = {
var count = 0
val w = grid(0).size
val h = grid.size
if(x > 0 && grid(x - 1)(y) == true) {
count = count + 1
}
if(x < (w - 1) && grid(x + 1)(y) == true) {
count = count + 1
}
if(y < (h - 1) && grid(y + 1)(y) == true) {
count = count + 1
}
if(y > 0 && grid(y - 1)(y) == true) {
count = count + 1
}
count
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment