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 OFF
<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
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, Text, vMsg, Enter Sudoku puzzle and click Solve
Gui, Add, Button, vButton gSolve w175 x10 Center, Solve
Gui, Show,, Sudoku Solver
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
Loop 9
{
r := A_Index
{
r := A_Index
Loop 9
{
Loop 9
b := (r*9)+A_Index-9
{
b := (r*9)+A_Index-9
GuiControl,, %r%%A_Index%, % a%b%
GuiControl, +ReadOnly, %r%%A_Index%
GuiControl,, %r%_%A_Index%, % a%b%
GuiControl, +ReadOnly, %r%_%A_Index%
}
}
}
}
GuiControl,, Msg, Solved! Time: %seconds%
if answer
GuiControl,, Button, Close
GuiControl,, Msg, Solved!`nTime: %seconds%s`nIterations: %iterations%
GuiControl, +gClose, Button
else
GuiControl,, Msg, Failed! :(`nTime: %seconds%s`nIterations: %iterations%
GuiControl,, Button, Again!
GuiControl, +gAgain, Button
return
return

GuiClose:
GuiClose:
ExitApp
Close:

ExitApp
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
f := Mod(f + 79, 81) + 1
copyf := f
GuiControl, Focus, Edit%f%
While copyf > 0
copyf -= 9
f := ((copyf = 1) ? f+9 : f-1)
f := ((f < 1) ? 81 : f)
GuiControl, Focus, Edit%f%
return
return
Next:
~Right::
~Right::
GuiControlGet, f, focus
GuiControlGet, f, focus
StringTrimLeft, f, f, 4
StringTrimLeft, f, f, 4
copyf := f
f := Mod(f, 81) + 1
GuiControl, Focus, Edit%f%
While copyf >= 9
copyf -= 9
f := ((copyf = 9) ? f-9 : f+1)
f := Mod(f, 81)
GuiControl, Focus, Edit%f%
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@]") ;format puzzle as single line string
p := RegExReplace(p, "[^1-9@]"), ErrorLevel := 0 ;format puzzle as single line string
return Sudoku_Display(Sudoku_Solve(p))
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)
If (d >= 81), ErrorLevel++
return p ;this is 82nd iteration, so it has successfully finished iteration 81
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
If InStr( "123456789", SubStr(p, d+1, 1) ) ;this depth is a given, skip through
return Sudoku_Solve(p, d+1)
return Sudoku_Solve(p, d+1)
m := Sudoku_Constraints(p,d) ;a string of this level's constraints.
m := Sudoku_Constraints(p,d) ;a string of this level's constraints.
; (these will not change for all 9 loops)
; (these will not change for all 9 loops)
Loop 9
Loop 9
{
{
If InStr(m, A_Index)
If InStr(m, A_Index)
Continue
Continue
NumPut(Asc(A_Index), p, d, "Char")
NumPut(Asc(A_Index), p, d, "Char")
If r := Sudoku_Solve(p, d+1)
If r := Sudoku_Solve(p, d+1)
return r
return r
}
}
return 0
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)
c := Mod(d,9)
, r := (d - c) // 9
, r := (d - c) // 9
, b := r//3*27 + c//3*3 + 1
, b := r//3*27 + c//3*3 + 1
;convert to 1-based
;convert to 1-based
, c++
, c++
return ""
return ""
; row:
; row:
. SubStr(p, r * 9 + 1, 9)
. SubStr(p, r * 9 + 1, 9)
; column:
; column:
. SubStr(p,c ,1) SubStr(p,c+9 ,1) SubStr(p,c+18,1)
. 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+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)
. SubStr(p,c+54,1) SubStr(p,c+63,1) SubStr(p,c+72,1)
;box
;box
. SubStr(p, b, 3) SubStr(p, b+9, 3) SubStr(p, b+18, 3)
. SubStr(p, b, 3) SubStr(p, b+9, 3) SubStr(p, b+18, 3)
}
}
Sudoku_Display( p ) {
Sudoku_Display( p ) {
If StrLen(p) = 81
If StrLen(p) = 81
loop 81
loop 81
r .= SubStr(p, A_Index, 1) . "|"
r .= SubStr(p, A_Index, 1) . "|"
return r
return r
}</lang>
}</lang>