Created
September 17, 2020 00:41
-
-
Save justinhj/9ece99e607a6e9403e53ba697ae4726e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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