Sudoku: Difference between revisions
Content deleted Content added
Go solution |
m →[[Sudoku#ALGOL 68]]: Translation of this D version |
||
Line 194: | Line 194: | ||
8 2 6 4 1 9 7 3 5</pre> |
8 2 6 4 1 9 7 3 5</pre> |
||
=={{header|D}}== |
|||
<lang d>import std.stdio; |
|||
bool[char]genFull() { |
|||
return ['1':true, |
|||
'2':true, |
|||
'3':true, |
|||
'4':true, |
|||
'5':true, |
|||
'6':true, |
|||
'7':true, |
|||
'8':true, |
|||
'9':true]; |
|||
} |
|||
// these three functions assume that the number has not already been found |
|||
bool[char]availRow(int x,int y,bool[char]available=genFull()) { |
|||
// x designates row, y designates column |
|||
foreach(ele;puzzle[x]) { |
|||
if (ele != '_') available.remove(ele); |
|||
} |
|||
return available; |
|||
} |
|||
bool[char]availCol(int x,int y,bool[char]available=genFull()) { |
|||
// x designates row, y designates column |
|||
for(int i = 0;i<9;i++) { |
|||
if (puzzle[i][y] != '_') available.remove(puzzle[i][y]); |
|||
} |
|||
return available; |
|||
} |
|||
bool[char]availBox(int x,int y,bool[char]available=genFull()) { |
|||
// x designates row, y designates column |
|||
// get a base index for the boxes |
|||
int bx = x/3;bx*=3; |
|||
int by = y/3;by*=3; |
|||
for(int i = 0;i<3;i++) for(int j = 0;j<3;j++) { |
|||
if (puzzle[bx+i][by+j] != '_') available.remove(puzzle[bx+i][by+j]); |
|||
} |
|||
return available; |
|||
} |
|||
char[][]puzzle; |
|||
void main() { |
|||
puzzle ~= "394__267_".dup; |
|||
puzzle ~= "___3__4__".dup; |
|||
puzzle ~= "5__69__2_".dup; |
|||
puzzle ~= "_45___9__".dup; |
|||
puzzle ~= "6_______7".dup; |
|||
puzzle ~= "__7___58_".dup; |
|||
puzzle ~= "_1__67__8".dup; |
|||
puzzle ~= "__9__8___".dup; |
|||
puzzle ~= "_264__735".dup; |
|||
while(1) { |
|||
bool done = true; |
|||
foreach(i,row;puzzle) foreach(j,ref ele;row) if (ele == '_') { |
|||
// poke at the elements that are _ |
|||
auto remaining = availBox(i,j,availRow(i,j,availCol(i,j))); |
|||
if (remaining.keys.length == 1) ele = remaining.keys[0]; |
|||
else done = false; |
|||
} |
|||
if (done) break; |
|||
} |
|||
// write out completed puzzle |
|||
writefln("Completed puzzle:"); |
|||
foreach(row;puzzle) writefln("%s",row); |
|||
}</lang> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
<lang AutoHotkey>#SingleInstance, Force |
<lang AutoHotkey>#SingleInstance, Force |