-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsudoku.php
56 lines (50 loc) · 1.12 KB
/
sudoku.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?
function createBoard($ct, $puzzle) {
$board = array();
$row = 0;
$col = 0;
$puzzle = explode(",", $puzzle);
for ($i = 0; $i < count($puzzle); $i++) {
$board[$row][$col] = $puzzle[$i];
$col++;
if (($i+1)%$ct == 0) {
$row++;
$col = 0;
}
}
return $board;
}
function validBoard($ct, $board) {
// check cols
for ($i = 0; $i < $ct; $i++) {
$values = implode("", range(1, $ct));
for ($j = 0; $j < $ct; $j++) {
$values = str_replace($board[$i][$j],"",$values);
}
if (trim($values) != "") {
return "False";
}
}
// check rows
for ($i = 0; $i < $ct; $i++) {
$values = implode("", range(1, $ct));
for ($j = 0; $j < $ct; $j++) {
$values = str_replace($board[$j][$i],"",$values);
}
if (trim($values) != "") {
return "False";
}
}
return "True";
}
$fh = fopen($argv[1], "r");
while (!feof($fh)) {
$test = trim(fgets($fh));
if ($test != "") {
$args = explode(";", $test);
$board = createBoard($args[0], $args[1]);
echo validBoard($args[0], $board) . "\n";
}
}
fclose($fh);
?>