Skip to content

Instantly share code, notes, and snippets.

@jezinka
Last active May 24, 2020 11:39
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 jezinka/9857f8cf21c6d09076312065fd2ad580 to your computer and use it in GitHub Desktop.
Save jezinka/9857f8cf21c6d09076312065fd2ad580 to your computer and use it in GitHub Desktop.
validation
private boolean isValid() {
boolean right = true
board.each { row ->
if (row.findAll { it != 0 }.countBy { it }.any { k, v -> v != 1 }) {
right = false
}
}
(0..this.board.size() - 1).each { colNum ->
List<Integer> column = board.collect { it[colNum] }
if (column.findAll { it != 0 }.countBy { it }.any { k, v -> v != 1 }) {
right = false
}
}
Map<String, List<Integer>> squares = [:]
board.eachWithIndex { List<Integer> row, int i ->
row.eachWithIndex { int entry, int j ->
String key = findSquare(i, j)
if (!squares.containsKey(key)) {
squares[key] = [entry]
} else {
squares[key] << entry
}
}
}
squares.values().each { square ->
if (square.findAll { it != 0 }.countBy { it }.any { k, v -> v != 1 }) {
right = false
}
}
return right
}
private String findSquare(int i, int j) {
int squareCount = Math.sqrt(board.size()) as Integer
return "${Math.floor(i / squareCount)}${Math.floor(j / squareCount)}".toString()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment