Sudoku: Difference between revisions
Content added Content deleted
m (→{{header|AutoHotkey}}: Fixed iterations, added readable indentation, minor display adjustments & bugfixes) |
|||
Line 4: | Line 4: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
<lang AutoHotkey>#SingleInstance |
<lang AutoHotkey>#SingleInstance, Force |
||
SetBatchLines, -1 |
SetBatchLines, -1 |
||
SetTitleMatchMode, 3 |
SetTitleMatchMode, 3 |
||
Loop 9 { |
Loop 9 { |
||
r := A_Index, y := r*17-8 |
|||
r := A_Index, y := r*17-8 + (A_Index >= 7 ? 4 : A_Index >= 4 ? 2 : 0) |
|||
⚫ | |||
Loop 9 { |
|||
c := A_Index, x := c*17+5 + (A_Index >= 7 ? 4 : A_Index >= 4 ? 2 : 0) |
|||
Gui, Add, Edit, x%x% y%y% w17 h17 v%r%%c% Center Number Limit1 |
Gui, Add, Edit, x%x% y%y% w17 h17 v%r%_%c% Center Number Limit1 gNext |
||
⚫ | |||
} |
|||
} |
|||
⚫ | |||
Gui, Add, Button, vButton gSolve w175 x10 Center, Solve |
|||
Gui, Add, |
Gui, Add, Button, vButton gSolve w175 x10 Center, Solve |
||
Gui, |
Gui, Add, Text, vMsg r3, Enter Sudoku puzzle and click Solve |
||
Gui, Show,, Sudoku Solver |
|||
Return |
Return |
||
Solve: |
Solve: |
||
Gui, Submit, NoHide |
Gui, Submit, NoHide |
||
Loop 9 |
Loop 9 |
||
⚫ | |||
{ |
|||
r := A_Index |
r := A_Index |
||
Loop 9 |
Loop 9 |
||
If (%r%%A_Index% = "") |
If (%r%_%A_Index% = "") |
||
puzzle .= "@" |
puzzle .= "@" |
||
Else |
Else |
||
puzzle .= %r%%A_Index% |
puzzle .= %r%_%A_Index% |
||
⚫ | |||
} |
|||
s := A_TickCount |
s := A_TickCount |
||
answer := Sudoku(puzzle) |
answer := Sudoku(puzzle) |
||
iterations := ErrorLevel |
|||
e := A_TickCount |
e := A_TickCount |
||
seconds := (e-s)/1000 |
seconds := (e-s)/1000 |
||
StringSplit, a, answer, | |
StringSplit, a, answer, | |
||
Loop 9 |
|||
⚫ | |||
{ |
|||
{ |
|||
r := A_Index |
|||
⚫ | |||
Loop 9 |
|||
{ |
|||
b := (r*9)+A_Index-9 |
|||
⚫ | |||
GuiControl, |
GuiControl,, %r%_%A_Index%, % a%b% |
||
⚫ | |||
⚫ | |||
} |
|||
} |
|||
} |
|||
⚫ | |||
if answer |
|||
⚫ | |||
⚫ | |||
⚫ | |||
else |
|||
GuiControl,, Msg, Failed! :(`nTime: %seconds%s`nIterations: %iterations% |
|||
⚫ | |||
⚫ | |||
return |
return |
||
GuiClose: |
GuiClose: |
||
⚫ | |||
Close: |
|||
⚫ | |||
Again: |
|||
Reload |
|||
#IfWinActive, Sudoku Solver |
#IfWinActive, Sudoku Solver |
||
~*Enter::GoSub % GetKeyState( "Shift", "P" ) ? "~Up" : "~Down" |
|||
~Up:: |
~Up:: |
||
GuiControlGet, f, focus |
GuiControlGet, f, focus |
||
StringTrimLeft, f, f, 4 |
StringTrimLeft, f, f, 4 |
||
f := ((f >= 1 && f <= 9) ? f+72 : f-9) |
f := ((f >= 1 && f <= 9) ? f+72 : f-9) |
||
GuiControl, Focus, Edit%f% |
GuiControl, Focus, Edit%f% |
||
return |
return |
||
~Down:: |
~Down:: |
||
GuiControlGet, f, focus |
GuiControlGet, f, focus |
||
StringTrimLeft, f, f, 4 |
StringTrimLeft, f, f, 4 |
||
f := ((f >= 73 && f <= 81) ? f-72 : f + 9) |
f := ((f >= 73 && f <= 81) ? f-72 : f + 9) |
||
GuiControl, Focus, Edit%f% |
GuiControl, Focus, Edit%f% |
||
return |
return |
||
~Left:: |
~Left:: |
||
GuiControlGet, f, focus |
GuiControlGet, f, focus |
||
StringTrimLeft, f, f, 4 |
StringTrimLeft, f, f, 4 |
||
⚫ | |||
copyf := f |
|||
⚫ | |||
While copyf > 0 |
|||
copyf -= 9 |
|||
f := ((copyf = 1) ? f+9 : f-1) |
|||
f := ((f < 1) ? 81 : f) |
|||
⚫ | |||
return |
return |
||
Next: |
|||
~Right:: |
~Right:: |
||
GuiControlGet, f, focus |
GuiControlGet, f, focus |
||
StringTrimLeft, f, f, 4 |
StringTrimLeft, f, f, 4 |
||
f := Mod(f, 81) + 1 |
|||
⚫ | |||
While copyf >= 9 |
|||
copyf -= 9 |
|||
f := ((copyf = 9) ? f-9 : f+1) |
|||
⚫ | |||
⚫ | |||
return |
return |
||
#IfWinActive |
#IfWinActive |
||
; Functions Start here |
; Functions Start here |
||
Sudoku( p ) { ;ErrorLevel contains the number of iterations |
Sudoku( p ) { ;ErrorLevel contains the number of iterations |
||
p := RegExReplace(p, "[^1-9@]"), ErrorLevel := 0 ;format puzzle as single line string |
|||
return Sudoku_Display(Sudoku_Solve(p)) |
|||
} |
} |
||
Line 103: | Line 107: | ||
; returns: 81 char string with non-givens replaced with valid solution |
; returns: 81 char string with non-givens replaced with valid solution |
||
; |
; |
||
If (d >= 81), ErrorLevel++ |
|||
return p ;this is 82nd iteration, so it has successfully finished iteration 81 |
|||
If InStr( "123456789", SubStr(p, d+1, 1) ) ;this depth is a given, skip through |
|||
return Sudoku_Solve(p, d+1) |
|||
m := Sudoku_Constraints(p,d) ;a string of this level's constraints. |
|||
; (these will not change for all 9 loops) |
|||
Loop 9 |
|||
{ |
|||
If InStr(m, A_Index) |
|||
Continue |
|||
NumPut(Asc(A_Index), p, d, "Char") |
|||
If r := Sudoku_Solve(p, d+1) |
|||
return r |
|||
} |
|||
return 0 |
|||
} |
} |
||
Sudoku_Constraints( ByRef p, d ) { |
Sudoku_Constraints( ByRef p, d ) { |
||
; returns a string of the constraints for a particular position |
; returns a string of the constraints for a particular position |
||
c := Mod(d,9) |
|||
, r := (d - c) // 9 |
|||
, b := r//3*27 + c//3*3 + 1 |
|||
;convert to 1-based |
|||
, c++ |
|||
return "" |
|||
; row: |
|||
. SubStr(p, r * 9 + 1, 9) |
|||
; column: |
|||
. SubStr(p,c ,1) SubStr(p,c+9 ,1) SubStr(p,c+18,1) |
|||
. SubStr(p,c+27,1) SubStr(p,c+36,1) SubStr(p,c+45,1) |
|||
. SubStr(p,c+54,1) SubStr(p,c+63,1) SubStr(p,c+72,1) |
|||
;box |
|||
. SubStr(p, b, 3) SubStr(p, b+9, 3) SubStr(p, b+18, 3) |
|||
} |
} |
||
Sudoku_Display( p ) { |
Sudoku_Display( p ) { |
||
If StrLen(p) = 81 |
|||
loop 81 |
|||
r .= SubStr(p, A_Index, 1) . "|" |
|||
return r |
|||
}</lang> |
}</lang> |
||