Skip to content

Instantly share code, notes, and snippets.

@myeesan
Created August 15, 2014 07:31
Show Gist options
  • Save myeesan/b380e0d6a7478a90a893 to your computer and use it in GitHub Desktop.
Save myeesan/b380e0d6a7478a90a893 to your computer and use it in GitHub Desktop.
object Sudoku {
val in = io.Source.fromFile("sample.in").getLines
case class Case(n: Int, _lines: List[String]) {
def lines: List[List[Int]] = {
_lines.map(_.split(" ").map(_.toInt).toList).toList
}
def linesVert: List[List[Int]] = {
val res = for {
i <- 0 until n * n
} yield lines(i)
res.toList
}
def sn2 = {
val seq = (0 until n * n).toList.sliding(n, n).map(_.zipWithIndex).toList.zipWithIndex
val res = for {
(is, s) <- seq
(ys, t) <- is
} yield lines(s)(t)
res
}
def check(l: List[Int]): Boolean = ((l.toSet.size == l.size) && l.forall(x => x > 0 && x < l.size))
def solve = {
val checkRes1 = lines.forall(check) && linesVert.forall(check)
val checkRes2 = check(sn2)
checkRes1 && checkRes2
}
}
def main(args: Array[String]) {
val nOfCases = in.next.toInt
val cases = for {
i <- 1 to nOfCases
val n = in.next.toInt
val lines = in.take(n * n).toList
} yield Case(n, lines)
cases.map(_ solve).map {
case true => "YES"
case false => "NO"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment