Introduction
A valid Sudoku grid is filled with numbers 1 to 9, with no number occurring more than once in each sub-block of 9, row or column. Read this article for further details if you're unfamiliar with this popular puzzle.
Challenge
The challenge is to write the shortest program that validates a Sudoku grid that might not be full.
Input will be a string of 9 lines of 9 characters each, representing the grid. An empty cell will be represented by a .
. Your output should be Valid
if the grid is valid, otherwise output Invalid
.
Example
Input
123...789
...456...
456...123
789...456
...123...
564...897
...231...
897...564
...564...
Output
Valid
Input
123456789
987654321
123456789
123456789
987654321
123456789
123456789
987654321
123456789
Output
Invalid
Code Golf Rules
Please post your shortest code in any language that solves this problem. Input and output may be handled via stdin and stdout or by other files of your choice.
Winner will be the shortest solution (by byte count) in a language with an implementation existing prior to the posting of this question. So while you are free to use a language you've just made up in order to submit a 0-byte solution, it won't count, and you'll probably get downvotes.
Perl: 186
Input is from stdin, output to stdout, linebreaks in input optional.
(Linebreaks added for "clarity".)
c()
is a function that checks the input in@y
against a list of lists of position numbers passed as an argument. It returns 0 if all position lists are valid (contain no number more than once) and 1 otherwise, using recursion to check each list. The bottom line builds this list of lists, passes it toc()
and uses the result to select the right prefix to output.One thing that I quite like is that this solution takes advantage of "self-similarity" in the "block" position list in
@b
(which is redundantly rebuilt many times to avoid having@b=...
in a separate statement): the top-left position of the ith block within the entire puzzle can be found by multiplying the ith element in@b
by 3.More spread out:
ASL: 108
ASL is a Golfscript inspired scripting language I made.
Perl, 153 char
@B
contains the 81 elements of the board.&E
tests whether a subset of@B
contains any duplicate digitsmain loop validates each column, "block", and row of the puzzle
Python:
159158<T> is a single tab character
Haskell:
207230218195172Common Lisp:
266252