Sudoku: Difference between revisions
Content deleted Content added
No edit summary |
Updated D version |
||
Line 1,595: | Line 1,595: | ||
{{trans|C++}} |
{{trans|C++}} |
||
<lang d>import std.stdio, std.range, std.string, std.algorithm, std.array; |
<lang d>import std.stdio, std.range, std.string, std.algorithm, std.array; |
||
enum int side = 9; // Sudoku grid side |
enum int side = 9; // Sudoku grid side |
||
alias immutable(char) TableItem; // Statically in '0'...'9' |
alias immutable(char) TableItem; // Statically in '0'...'9' |
||
alias TableItem[side ^^ 2] SudokuTable; |
alias TableItem[side ^^ 2] SudokuTable; |
||
SudokuTable sudokuSolver(const ref SudokuTable problem) |
SudokuTable sudokuSolver(const ref SudokuTable problem) |
||
in { |
in { |
||
Line 1,608: | Line 1,608: | ||
} body { |
} body { |
||
uint[side ^^ 2] grid = array(map!q{ a - '0' }(problem[])); |
uint[side ^^ 2] grid = array(map!q{ a - '0' }(problem[])); |
||
bool checkValidity(in int val, in int x, in int y) nothrow { |
bool checkValidity(in int val, in int x, in int y) nothrow { |
||
foreach (i; 0 .. side) |
foreach (i; 0 .. side) |
||
if (grid[y*side + i] == val || grid[i*side + x] == val) |
if (grid[y*side + i] == val || grid[i*side + x] == val) |
||
return false; |
return false; |
||
immutable int startX = (x / 3) * 3; |
immutable int startX = (x / 3) * 3; |
||
immutable int startY = (y / 3) * 3; |
immutable int startY = (y / 3) * 3; |
||
Line 1,622: | Line 1,622: | ||
return true; |
return true; |
||
} |
} |
||
static class FinishedSudokuException: Exception { |
static class FinishedSudokuException: Exception { |
||
this(string msg) { |
this(string msg) { |
||
Line 1,628: | Line 1,628: | ||
} |
} |
||
} |
} |
||
void placeNumber(in int pos) { |
void placeNumber(in int pos) { |
||
if (pos == side ^^ 2) |
if (pos == side ^^ 2) |
||
Line 1,636: | Line 1,636: | ||
return; |
return; |
||
} |
} |
||
foreach (n; 1 .. side+1) |
foreach (n; 1 .. side+1) |
||
if (checkValidity(n, pos % side, pos / side)) { |
if (checkValidity(n, pos % side, pos / side)) { |
||
Line 1,644: | Line 1,644: | ||
} |
} |
||
} |
} |
||
try { |
try { |
||
placeNumber(0); |
placeNumber(0); |
||
Line 1,664: | Line 1,664: | ||
} body { |
} body { |
||
string result; |
string result; |
||
foreach (i; 0 .. side) { |
foreach (i; 0 .. side) { |
||
foreach (j; 0 .. side) { |
foreach (j; 0 .. side) { |
||
Line 1,676: | Line 1,676: | ||
result ~= "------+-------+------\n"; |
result ~= "------+-------+------\n"; |
||
} |
} |
||
return result; |
return result; |
||
} |
} |
||
void main() { |
void main() { |
||
immutable SudokuTable problem = |
|||
"850002400" ~ |
"850002400" ~ |
||
"720000009" ~ |
"720000009" ~ |
||
Line 1,693: | Line 1,692: | ||
"000036040"; |
"000036040"; |
||
writeln(formatSudoku(problem)); |
writeln(formatSudoku(problem)); |
||
immutable solution = sudokuSolver(problem); |
|||
if (solution[0] == TableItem.init) |
if (solution[0] == TableItem.init) |
||
writeln("Unsolvable!"); |
writeln("Unsolvable!"); |