My issue is connected with efficient way to count elements with false in the array of arrays. I now how iterate each array and count if element is true or false i am looking more efficient way.
Example of the problem:
let n = 4
var board = Array(repeating:Array(repeating:true, count:n), count:n)
let qPos = [4,4]
board[qPos[0] - 1][qPos[1] - 1] = false
for x in 0..<n{
for y in 0..<n {
if (x == qPos[0] - 1) {
board[x][y] = false
}
if (y == qPos[1] - 1) {
board[x][y] = false
}
if (y == x) {
board[x][y] = false
}
}
}
//How to calculate all negative elements of the board ? In this code output should be 10
My example of count:
var count = 0
for x in 0..<board.count {
for y in 0..<board.count {
if board[x][y] == false {
count += 1
}
}
}
board.forEach { $0.forEach { count += !$0 ? 1 : 0 } }