Created
May 23, 2012 21:26
-
-
Save rodhilton/2777936 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
| /* | |
| * 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