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:
168128The first regex checks for duplicates that are in the same row and column; the second regex handles duplicates in the "same box".
Further improvement is possible by replacing the
\n
in the first regex with a literal newline (1 char), or with >= Perl 5.12, replacing[^\n]
with\N
(3 char)Earlier, 168 char solution: Input is from stdin, output is to stderr because it makes things so easy. Linebreaks are optional and not counted.
Python: 140
C:
165162161160159The two newlines are not needed. One char saved by josefx :-) ...
Lua, 341 bytes
Although I know that Lua isn't the best golfing language, however, considering it's size, I think it's worth posting it ;). Non-golfed, commented and error-printing version, for extra fun :)
Golfed version, 341 bytes
Python:
230221200185First the readable version at len=199:
Since SO doesn't display tab characters, I've used
<T>
to represent a single tab character.PS. the same approach minEvilized down to 185 chars:
Perl: 202
I'm reading Modern Perl and felt like coding something... (quite a cool book by the way:)
Count is excluding unnecessary newlines. This may require Perl 5.12.2.
A bit more readable: