Skip to content

Instantly share code, notes, and snippets.

@sungkmi
Created August 15, 2014 07:28
Show Gist options
  • Save sungkmi/3f225d2c31b6275f92c4 to your computer and use it in GitHub Desktop.
Save sungkmi/3f225d2c31b6275f92c4 to your computer and use it in GitHub Desktop.
object SudokuChecker extends App {
def check(matrix: Vector[Vector[Int]]): Boolean = {
val nSq = matrix.size
val n = math.sqrt(nSq).toInt
val ans = (1 to nSq).toSet
(0 until nSq).forall { i: Int =>
(for (i1 <- 0 until n; j1 <- 0 until n)
yield matrix(i1 + n * (i / n))(j1 + n * (i % n))).toSet == ans &&
matrix(i).toSet == ans &&
(Set.empty[Int] /: matrix)(_ + _(i)) == ans
}
}
def process(lineIn: Iterator[String])(lineOut: String => Unit) =
for (i <- 1 to lineIn.next().toInt) {
val n = lineIn.next().toInt
val m = Vector.fill(n * n)(lineIn.next().split(' ').map(_.toInt).toVector)
lineOut(s"Case #$i: ${if (check(m)) "Yes" else "No"}")
}
val writer = new java.io.PrintWriter("a.large.out")
try {
process(io.Source.fromFile("A-large-practice.in").getLines)(writer.println)
} finally {
writer.flush(); writer.close()
}
}
import org.scalatest._
import SudokuChecker._
class SudokuCheckerTest extends FunSuite {
import language.implicitConversions
implicit def linesToMatrix(lines: String): Vector[Vector[Int]] =
lines.lines.toVector.map(_.split(' ').map(_.toInt).toVector)
test("sample #1") {
val m = """5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 3 4 8
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 5 3 7 9 1
7 1 3 9 2 4 8 5 6
9 6 1 5 3 7 2 8 4
2 8 7 4 1 9 6 3 5
3 4 5 2 8 6 1 7 9"""
assert(check(m) === true)
}
test("sample #2") {
val m = """1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9"""
assert(check(m) === false)
}
test("sample #3") {
val m = """5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 3 4 8
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 999 3 7 9 1
7 1 3 9 2 4 8 5 6
9 6 1 5 3 7 2 8 4
2 8 7 4 1 9 6 3 5
3 4 5 2 8 6 1 7 9"""
assert(check(m) === false)
}
test("sample case") {
val input = """3
3
5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 3 4 8
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 5 3 7 9 1
7 1 3 9 2 4 8 5 6
9 6 1 5 3 7 2 8 4
2 8 7 4 1 9 6 3 5
3 4 5 2 8 6 1 7 9
3
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
3
5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 3 4 8
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 999 3 7 9 1
7 1 3 9 2 4 8 5 6
9 6 1 5 3 7 2 8 4
2 8 7 4 1 9 6 3 5
3 4 5 2 8 6 1 7 9""".lines
val expected = """Case #1: Yes
Case #2: No
Case #3: No""".lines
process(input) { s =>
for (line <- s.lines)
assert(line === expected.next())
}
}
test("full small case") {
val input = io.Source.fromFile("A-small-practice.in").getLines()
val expected = io.Source.fromFile("a.small.out.ref").getLines()
process(input) { s =>
for (line <- s.lines)
assert(line === expected.next())
}
}
test("full large case") {
val input = io.Source.fromFile("A-large-practice.in").getLines()
val expected = io.Source.fromFile("a.large.out.ref").getLines()
process(input) { s =>
for (line <- s.lines)
assert(line === expected.next())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment