Skip to content

Instantly share code, notes, and snippets.

@rodhilton
Created May 23, 2012 21:26
Show Gist options
  • Select an option

  • Save rodhilton/2777936 to your computer and use it in GitHub Desktop.

Select an option

Save rodhilton/2777936 to your computer and use it in GitHub Desktop.
/*
* Determines if a Sudoku board (a list of lists of Cell case classes containing Options for the
* cell value) is filled in incorrectly
*/
def isScrewedUp: Boolean = {
(0 to 8).foldLeft(false) {
(b, i) => b || rowScrewedUp(i) || colScrewedUp(i) || boxScrewedUp(i)
}
}
private def rowScrewedUp(rowNum: Int): Boolean = {
cellsScrewedUp(board(rowNum))
}
private def colScrewedUp(colNum: Int): Boolean = {
cellsScrewedUp(board.transpose.toSeq(colNum))
}
private def boxScrewedUp(boxNum: Int): Boolean = {
val boxRow = boxNum / 3
val boxCol = boxNum % 3
//Get the relevant rows
val rows = board.view.slice(boxRow * 3, boxRow * 3 + 3)
//Take the intersection of the rows with the columns
val box = rows.map{row => row.slice(boxCol * 3, boxCol * 3 + 3)}.flatten
cellsScrewedUp(box)
}
private def cellsScrewedUp(set: Seq[Cell]) = {
val values = set.map {
cell => cell.value
}
val flat = values.flatten
flat.distinct.size != flat.size
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment