Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

code duplication removed

  • Loading branch information...
commit 17079c6fa509875dca2a8af21ec23b2f9cb1d3de 1 parent 98bfe29
@maruks authored
View
18 src/main/scala/sudoku/SudokuSolver.scala
@@ -3,6 +3,7 @@ package sudoku
object SudokuSolver {
def solve(p: Vector[Int]): Option[Vector[Int]] = {
+
smallestChangeSet(p) match {
case None => Some(p)
case Some(set) if set._2.isEmpty => None
@@ -19,22 +20,21 @@ object SudokuSolver {
}
def possibleValues(p: Vector[Int], i: Int) = {
- (1 to 9).toSet -- (selectRow(p, i) ++ selectColumn(p, i) ++ selectSquare(p, i))
+ def select(s : Symbol) = (indexFun(s))(i).map(p(_)).toSet[Int]
+ (1 to 9).toSet -- (select('row) ++ select('column) ++ select('square))
}
- def selectRow(p: Vector[Int], i: Int) = {
- (i - i % 9 to i - i % 9 + 8).map(p(_)).toSet[Int]
- }
+ val indexFun = Map('row -> selectRow _, 'column -> selectColumn _, 'square -> selectSquare _)
- def selectColumn(p: Vector[Int], i: Int) = {
- (i % 9 to i % 9 + 72 by 9).map(p(_)).toSet[Int]
- }
+ def selectRow(i: Int) = (i - i % 9 to i - i % 9 + 8)
+
+ def selectColumn(i: Int) = (i % 9 to i % 9 + 72 by 9)
- def selectSquare(p: Vector[Int], i: Int) = {
+ def selectSquare(i: Int) = {
val x = i % 9 - (i % 9) % 3
val y = i / 9 - (i / 9) % 3
val spos = x + y * 9
val r = (spos to spos + 2)
- (r ++ r.map(_ + 9) ++ r.map(_ + 18)).map(p(_)).toSet[Int]
+ r ++ r.map(_ + 9) ++ r.map(_ + 18)
}
}
View
70 src/test/scala/sudoku/SudokuSolverSpec.scala
@@ -11,65 +11,69 @@ class SudokuSolverSpec extends WordSpec with ShouldMatchers {
val p = SudokuIO.readFile(new File("src/test/resources/puzzle2.txt"))
+ val indexFun = Map('row -> SudokuSolver.selectRow _, 'column -> SudokuSolver.selectColumn _, 'square -> SudokuSolver.selectSquare _)
+
+ def select(s : Symbol, p : Vector[Int], i : Int) = (indexFun(s))(i).map(p(_)).toSet[Int]
+
"select first row" in {
val set = Set(2, 0, 8, 6, 7)
- SudokuSolver.selectRow(p, 0) should equal (set)
- SudokuSolver.selectRow(p, 3) should equal (set)
- SudokuSolver.selectRow(p, 8) should equal (set)
+ select('row, p, 0) should equal (set)
+ select('row, p, 3) should equal (set)
+ select('row, p, 8) should equal (set)
}
"select second row" in {
val set = Set(3, 0, 2, 5)
- SudokuSolver.selectRow(p, 9) should equal (set)
- SudokuSolver.selectRow(p, 14) should equal (set)
- SudokuSolver.selectRow(p, 17) should equal (set)
+ select('row, p, 9) should equal (set)
+ select('row, p, 14) should equal (set)
+ select('row, p, 17) should equal (set)
}
"select last row" in {
val set = Set(2, 0, 6, 8, 9)
- SudokuSolver.selectRow(p, 72) should equal (set)
- SudokuSolver.selectRow(p, 76) should equal (set)
- SudokuSolver.selectRow(p, 80) should equal (set)
+ select('row, p, 72) should equal (set)
+ select('row, p, 76) should equal (set)
+ select('row, p, 80) should equal (set)
}
"select first column" in {
val set = Set(2, 0, 5, 9)
- SudokuSolver.selectColumn(p, 0) should equal (set)
- SudokuSolver.selectColumn(p, 27) should equal (set)
- SudokuSolver.selectColumn(p, 72) should equal (set)
+ select('column, p, 0) should equal (set)
+ select('column, p, 27) should equal (set)
+ select('column, p, 72) should equal (set)
}
"select second column" in {
val set = Set(0, 3, 6)
- SudokuSolver.selectColumn(p, 1) should equal (set)
- SudokuSolver.selectColumn(p, 28) should equal (set)
- SudokuSolver.selectColumn(p, 73) should equal (set)
+ select('column, p, 1) should equal (set)
+ select('column, p, 28) should equal (set)
+ select('column, p, 73) should equal (set)
}
"select last column" in {
val set = Set(7, 0, 3, 4, 9)
- SudokuSolver.selectColumn(p, 8) should equal (set)
- SudokuSolver.selectColumn(p, 35) should equal (set)
- SudokuSolver.selectColumn(p, 80) should equal (set)
+ select('column, p, 8) should equal (set)
+ select('column, p, 35) should equal (set)
+ select('column, p, 80) should equal (set)
}
"select upper left square" in {
val set = Set(2, 3, 5, 6, 0)
- SudokuSolver.selectSquare(p, 0) should equal (set)
- SudokuSolver.selectSquare(p, 10) should equal (set)
- SudokuSolver.selectSquare(p, 20) should equal (set)
+ select('square, p, 0) should equal (set)
+ select('square, p, 10) should equal (set)
+ select('square, p, 20) should equal (set)
}
"select middle right square" in {
val set = Set(9, 3, 1, 0)
- SudokuSolver.selectSquare(p, 33) should equal (set)
- SudokuSolver.selectSquare(p, 43) should equal (set)
- SudokuSolver.selectSquare(p, 53) should equal (set)
+ select('square, p, 33) should equal (set)
+ select('square, p, 43) should equal (set)
+ select('square, p, 53) should equal (set)
}
"select bottom right square" in {
val set = Set(2, 4, 1, 9, 0)
- SudokuSolver.selectSquare(p, 60) should equal (set)
- SudokuSolver.selectSquare(p, 61) should equal (set)
- SudokuSolver.selectSquare(p, 62) should equal (set)
- SudokuSolver.selectSquare(p, 69) should equal (set)
- SudokuSolver.selectSquare(p, 70) should equal (set)
- SudokuSolver.selectSquare(p, 71) should equal (set)
- SudokuSolver.selectSquare(p, 78) should equal (set)
- SudokuSolver.selectSquare(p, 79) should equal (set)
- SudokuSolver.selectSquare(p, 80) should equal (set)
+ select('square, p, 60) should equal (set)
+ select('square, p, 61) should equal (set)
+ select('square, p, 62) should equal (set)
+ select('square, p, 69) should equal (set)
+ select('square, p, 70) should equal (set)
+ select('square, p, 71) should equal (set)
+ select('square, p, 78) should equal (set)
+ select('square, p, 79) should equal (set)
+ select('square, p, 80) should equal (set)
}
"solve third sudoku" in {
Please sign in to comment.
Something went wrong with that request. Please try again.