Skip to content

Instantly share code, notes, and snippets.

@MachinesAreUs
Created November 19, 2014 18:39
Show Gist options
  • Save MachinesAreUs/13a772816ca7efa742b4 to your computer and use it in GitHub Desktop.
Save MachinesAreUs/13a772816ca7efa742b4 to your computer and use it in GitHub Desktop.
Mineswipper solution, enhancement to https://gist.github.com/hgmiguel/ac95336b805d22c97f6a
file = new File(args[0])
board = [][]
file.eachWithIndex {obj, i ->
if (i == 0) {
(rows, columns) = obj.split(" ")*.toInteger()
} else {
board << ([0] << obj.collect{ it == '*'?'*':0 } << [0]).flatten()
}
}
zero_row = (0..columns+1).collect {0}
board.add(0, zero_row)
board << zero_row
def isMine(row, col) {
board[row][col] == '*'
}
(1..rows).each { row ->
(1..columns).each { column ->
if(!isMine(row, column)) {
neighbours = [[r:row+1, c:column ],
[r:row-1, c:column ],
[r:row , c:column+1],
[r:row , c:column-1],
[r:row+1, c:column+1],
[r:row-1, c:column-1],
[r:row+1, c:column-1],
[r:row-1, c:column+1]]
board[row][column] += neighbours.findAll { isMine(it['r'], it['c'])}.size
}
}
}
board[1..rows].each {row ->
println row.collect { it.toString() }.join()[1..columns]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment