Created
January 14, 2013 04:39
-
-
Save FrancisVarga/4527807 to your computer and use it in GitHub Desktop.
check 9 numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php -f | |
<?php | |
$sudoko [] = [1, 8, 2, 5, 4, 3, 6, 9, 7]; | |
$sudoko [] = [9, 6, 5, 1, 7, 8, 3, 4, 2]; | |
$sudoko [] = [7, 4, 3, 9, 6, 2, 8, 1, 5]; | |
function isValidSudoko($sudoko) | |
{ | |
$sudLine = 0; | |
$sudCol = 0; | |
foreach ($sudoko as $line) { | |
$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]; | |
$sudLine++; | |
if (count($line) == 9) { | |
foreach ($line as $number) { | |
$sudCol++; | |
$index = array_search($number, $numbers); // need the index of the item, in_array doesnt work returns only bool | |
if ($index === false) { | |
throw new Exception("Sudoko is not valid. => " . $sudLine . "/" . $sudCol); | |
} | |
unset($numbers[$index]); // unset the item if found | |
} | |
} else { | |
throw new Exception("Sudoko is not valid. => " . $sudLine . "/" . $sudCol); | |
} | |
$sudCol = 0; | |
} | |
echo "Sudoko is valid." . PHP_EOL; | |
return true; | |
} | |
isValidSudoko($sudoko); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment