Skip to content

Instantly share code, notes, and snippets.

@FrancisVarga
Created January 14, 2013 04:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FrancisVarga/4527807 to your computer and use it in GitHub Desktop.
Save FrancisVarga/4527807 to your computer and use it in GitHub Desktop.
check 9 numbers
#!/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