21
4
Create a Sudoku solution CHECKER
There are oodles of Sudoku SOLVERS here, but I want you to create a solution CHECKER as small as humanly possible (code-golf).
A valid entry will be able to either take a 9x9 array as an argument (passed by reference, serialized on the command line, or however you want to take it) or accept an input file that is nine lines of nine numbers for the final grid. See examples of input below.
Valid input should be base-10 numbers (1-9)
Missing, empty, extra, non-numeric positions, or positions with numbers outside of 1-9 should be rejected as invalid input by returning a non-zero result, printing an error, or both.
Your program needs to test whether each number appears once per column, once per line, and once per 3x3 sub-grid. If it passes, return "0" and if not, return a non-zero result.
Use of external resources (websites, etc.) is to be avoided.
If your solution is a stand-alone program, exiting with a exit status of, or printing, "0" or non-zero for "Pass" or "Fail", respectively, is ok.
Let the smallest answer win!
Input Examples:
c array:
int input[9][9]={{1,2,3,4,5,6,7,8,9},
{4,5,6,7,8,9,1,2,3},
{7,8,9,1,2,3,4,5,6},
{2,3,1,5,6,4,8,9,7},
{5,6,4,8,9,7,2,3,1},
{8,9,7,2,3,1,5,6,4},
{3,1,2,6,4,5,9,7,8},
{6,4,5,9,7,8,3,1,2},
{9,7,8,3,1,2,6,4,5}
};
file:
123456789
456789123
789123456
231564897
564897231
897231564
312645978
645978312
978312645
The 9 sub-grids:
+---+---+---+
|123|456|789|
|456|789|123|
|789|123|456|
+---+---+---+
|231|564|897|
|564|897|231|
|897|231|564|
+---+---+---+
|312|645|978|
|645|978|312|
|978|312|645|
+---+---+---+
I like that your code's non-zero output is more meaningful than just
1
or-1
– David Wilkins – 2014-03-03T17:02:07.223I really liked your answer but in the end I elected not to go with a golfscript solution. I really hope you understand – David Wilkins – 2014-03-10T14:45:39.330
2@DavidWilkins Actually I don't understand - you made the rules (!) and didn't state anywhere that GolfScript was not allowed. – Howard – 2014-03-10T14:51:28.083
Your point is entirely valid...in truth I have nothing to justify not choosing your answer. Well played – David Wilkins – 2014-03-10T14:53:18.970