1.

Solve : Sudoku glitch?

Answer»

This has REALLY GOT me stumped. I created a sudoku-solving script (still in the works) that's getting a really weird issue.

The script can successfully solve this puzzle:
Quote

4_153_68_
639____51
_7_6412_9
___152_74
782___915
14_798___
2_7486_9_
39____768
_16_735_2
(By the way, this is the answer I got when I ran the script)Quote
4 2 1 5 3 9 6 8 7
6 3 9 8 2 7 4 5 1
5 7 8 6 4 1 2 3 9
9 6 3 1 5 2 8 7 4
7 8 2 3 6 4 9 1 5
1 4 5 7 9 8 3 2 6
2 5 7 4 8 6 1 9 3
3 9 4 2 1 5 7 6 8
8 1 6 9 7 3 5 4 2

But I get a weird issue when I try this puzzle: (which can be solved, ACCORDING to http://www.sudokusolver.co.uk/)
Quote
7_4_2__96
_6_913___
2__7__58_
__62__138
_8_3_9_7_
543__82__
_58_____7
___497___
39__8_6_2
Issue:
Quote
7 _ 4 _ 2 _ _ 9 6 <-- No spots filled, even though it pauses (meaning it's done)
_ 6 _ 9 1 3 _ _ _
2 _ _ 7 _ _ 5 8 _
_ 7 6 2 _ _ 1 3 8
_ 8 _ 3 _ 9 4 7 _
5 4 3 _ _ 8 2 _ _
_ 5 8 _ _ _ _ _ 7
_ _ _ 4 9 7 _ _ _
3 9 _ _ 8 _ 6 _ 2
0,1 is in 1 <-- Should not remain for more less than a second
0,3 is in 2
0,5 is in 2
0,6 is in 3
1,0 is in 1
1,2 is in 1
1,6 is in 3
1,7 is in 3
1,8 is in 3
2,1 is in 1
2,2 is in 1
2,4 is in 2
2,5 is in 2
2,8 is in 3
3,0 is in 4
3,4 is in 5
3,5 is in 5
4,0 is in 4
4,2 is in 4
4,4 is in 5
4,8 is in 6
5,3 is in 5
5,4 is in 5
5,7 is in 6
5,8 is in 6
6,0 is in 7
6,3 is in 8
6,4 is in 8
6,5 is in 8
6,6 is in 9
6,7 is in 9
7,0 is in 7
7,1 is in 7
7,2 is in 7
7,6 is in 9
7,7 is in 9
7,8 is in 9
8,2 is in 7
8,3 is in 8
8,5 is in 8
8,7 is in 9
Press any key to continue . . .

And for some other puzzles, it messes up and says one of the numbers is "_=".
Puzzle:
Quote
4_3__9168
19_643_5_
_7_1_2_93
935_6_8_1
__28459__
8_7_2_635
26_4_7_8_
_8_216_74
7543__2_6
Result:
Quote
4 2 3 5 7 9 1 6 8
1 9 8 6 4 3 7 5 2
5 7 6 1 8 2 4 9 3
9 3 5 7 6 4 8 2 1
6 1 2 8 4 5 9 _= 7
8 4 7 9 2 1 6 3 5
2 6 1 4 5 7 3 8 9
3 8 9 2 1 6 5 7 4
7 5 4 3 9 8 2 1 6
Press any key to continue . . .

Anyway, here is the script I'm using:
Code: [Select]@echo off
setlocal enabledelayedexpansion
set line0=4_153_68_
set line1=639____51
set line2=_7_6412_9
set line3=___152_74
set line4=782___915
set line5=14_798___
set line6=2_7486_9_
set line7=39____768
set line8=_16_735_2
::set num=0
::for /f "delims=" %%A in (sudoku4.txt) do set line!num!=%%A & set /a num+=1
for /l %%L in (0,1,8) do (
for /l %%C in (0,1,8) do (
set l%%Lc%%C=!line%%L:~%%C,1!
)
)

:re
call :display
for /l %%L in (0,1,8) do (
for /l %%C in (0,1,8) do (
if !l%%Lc%%C!==_ (
set l%%Lc%%Cposs=123456789
for /l %%Z in (0,1,8) do (
for %%P in (!l%%Lc%%Z!) do (
set l%%Lc%%Cposs=!l%%Lc%%Cposs:%%P=!
)
)
for /l %%Z in (0,1,8) do (
for %%P in (!l%%Zc%%C!) do (
set l%%Lc%%Cposs=!l%%Lc%%Cposs:%%P=!
)
)
::check box
if %%L LEQ 2 (
if %%C LEQ 2 (
set box=1
) else if %%C LEQ 5 (
set box=2
) else (
set box=3
)
) else if %%L LEQ 5 (
if %%C LEQ 2 (
set box=4
) else if %%C LEQ 5 (
set box=5
) else (
set box=6
)
) else (
if %%C LEQ 2 (
set box=7
) else if %%C LEQ 5 (
set box=8
) else (
set box=9
)
)
::echo %%L,%%C is in !box!
if box==1 for %%P in (!l0c0! !l0c1! !l0c2! !l1c0! !l1c1! !l1c2! !l2c0! !l2c1! !l2c2!) do set l%%@c%%Cposs=!l%%@c%%Cposs:%%P=!
if box==2 for %%P in (!l3c0! !l3c1! !l3c2! !l4c0! !l4c1! !l4c2! !l5c0! !l5c1! !l5c2!) do set l%%@c%%Cposs=!l%%@c%%Cposs:%%P=!
if box==3 for %%P in (!l6c0! !l6c1! !l6c2! !l7c0! !l7c1! !l7c2! !l8c0! !l8c1! !l8c2!) do set l%%@c%%Cposs=!l%%@c%%Cposs:%%P=!
if box==4 for %%P in (!l0c3! !l0c4! !l0c5! !l1c3! !l1c4! !l1c5! !l2c3! !l2c4! !l2c5!) do set l%%@c%%Cposs=!l%%@c%%Cposs:%%P=!
if box==5 for %%P in (!l3c3! !l3c4! !l3c5! !l4c3! !l4c4! !l4c5! !l5c3! !l5c4! !l5c5!) do set l%%@c%%Cposs=!l%%@c%%Cposs:%%P=!
if box==6 for %%P in (!l6c3! !l6c4! !l6c5! !l7c3! !l7c4! !l7c5! !l8c3! !l8c4! !l8c5!) do set l%%@c%%Cposs=!l%%@c%%Cposs:%%P=!
if box==7 for %%P in (!l0c6! !l0c7! !l0c8! !l1c6! !l1c7! !l1c8! !l2c6! !l2c7! !l2c8!) do set l%%@c%%Cposs=!l%%@c%%Cposs:%%P=!
if box==8 for %%P in (!l3c6! !l3c7! !l3c8! !l4c6! !l4c7! !l4c8! !l5c6! !l5c7! !l5c8!) do set l%%@c%%Cposs=!l%%@c%%Cposs:%%P=!
if box==9 for %%P in (!l6c6! !l6c7! !l6c8! !l7c6! !l7c7! !l7c8! !l8c6! !l8c7! !l8c8!) do set l%%@c%%Cposs=!l%%@c%%Cposs:%%P=!
ping localhost -n 1 > nul
call :length !l%%Lc%%Cposs!
if !len!==1 (
set l%%Lc%%C=!l%%Lc%%Cposs!
goto re
)
)
)
)
pause
exit


:length
set #=%1
set len=0
:lenloop
if defined # (set #=%#:~1%&set /A len += 1&goto lenloop)
goto :eof

:display
cls
echo.
for /l %%L in (0,1,8) do (
set line=
for /l %%C in (0,1,8) do (
set line=!line! !l%%Lc%%C!
)
echo !line!
)
As you can see, it solves the puzzle in the code fairly quickly, but like I said, it doesn't work for some. I've tried 5 different puzzles, but those are the issues I get.Oh dear... a sodoku solver in

batch?

Well, the first thing that leaps out at me is that there is no way you are using all the different algorithms which are required to reliably solve sodoku puzzles. Brute force/CRME will solve some, but not all; I can't really tell but I'm pretty sure you are only doing a brute force attempt to solve, and aren't performing any checks for lone rangers or twins.

For your first "weird" issue puzzle, my program gives me this:



I have no idea if it's right or not. I'm surprised it even looks like it worked, I would have practically bet money that it either crashed or only half-filled the board- I guess I don't have a lot of faith in any of my old VB6 programs anymore...

As for your puzzle that "messes up"... it's not even a valid puzzle. the 5th column has two fours in the initial setup. the "result" still has two fours, but no doubt the fact that there were two fours has caused the equals sign issue. I wouldn't have even noticed if my program didn't PREVENT me from selecting four for the second instance . You can discount that "problem" entirely since it's not even a valid puzzle to start with.

any other problems are almost certainly because you are only using CRME (Column-Row Minigrid Elimination) and aren't dealing with Lone Rangers or twins; I gave up on mine when it couldn't complete some of the "hard" puzzles from a puzzle book; no doubt I need to implement triplets to get those to be solved problem-free.

Wow, I'm surprised I didn't realize that the puzzles were wrong originally. I realized that I wouldn't be able to solve every puzzle, but thanks for your help anyway.Here's a very good example of what you're talking about BC.
Puzzle:
Quote
__91_7_42
2_48__51_
_5149_3__
1__7632__
59__8__61
_26__9__8
96___81_7
___671924
4179___8_

Solution from the script:
Quote
6 3 9 1 5 7 8 4 2
2 7 4 8 3 6 5 1 9
8 5 1 4 9 2 3 7 6
1 4 8 7 6 3 2 9 5
5 9 _ _ 8 4 7 6 1
7 2 6 _ 1 9 4 _ 8
9 6 _ _ 4 8 1 _ 7
3 8 5 6 7 1 9 2 4
4 1 7 9 2 5 6 8 3
Press any key to continue . . .

Solution from the script (and me):
Quote
6 3 9 | 1 5 7 | 8 4 2
2 7 4 | 8 3 6 | 5 1 9
8 5 1 | 4 9 2 | 3 7 6
------|-------|------
1 4 8 | 7 6 3 | 2 9 5
5 9 3 | 2 8 4 | 7 6 1
7 2 6 | 5 1 9 | 4 3 8
------|-------|------
9 6 2 | 3 4 8 | 1 5 7
3 8 5 | 6 7 1 | 9 2 4
4 1 7 | 9 2 5 | 6 8 3
Press any key to continue . . .
Mine solved it! yay.

Sorry I'm just surprised at how well it seems to work.

What's really weird, my script should have solved the puzzle I just posted...

Quote

6 3 9 | 1 5 7 | 8 4 2
2 7 4 | 8 3 6 | 5 1 9
8 5 1 | 4 9 2 | 3 7 6
------|-------|------
1 4 8 | 7 6 3 | 2 9 5
5 9 _ | _ 8 4 | 7 6 1
7 2 6 | _ 1 9 | 4 _ 8
------|-------|------
9 6 _ | _ 4 8 | 1 _ 7
3 8 5 | 6 7 1 | 9 2 4
4 1 7 | 9 2 5 | 6 8 3
Press any key to continue . . .


Discussion

No Comment Found