Command line .bat to solve Sudoku puzzle
[page last modified 2022-04-07]

This is a Windows command line .bat to solve a Sudoku puzzle.  It uses a brute force method (try all possible numbers) so it's very slow!  I wrote it just to see if it was possible to do in a batch script.  No external commands are used, only internal (SET, CALL, IF, FOR, etc).  There are no GOTO's in the script.

Enter just phsud to get a help message; or, supply the puzzle on the command line, using . or 0 to indicate an empty square (spaces are ignored):


   C>phsud  ...63.9..  .....47.3  1....7.6.  .875..2..  4.2...3.5  ..6..287.  .7.2....4  8.17.....  ..9.81...

Missing trailing squares are assumed empty, so this is also valid:

   C>phsud 1.23

If the argument exists as a file the unsolved puzzle is read from the file:

   C>phsud board

The puzzle is not checked; it is assumed to be valid.  "Solved" simply means a 1-9 could be legally placed in all empty squares.

The phsud.bat file (which can be download via a link beneath this listing):

@echo off& setlocal enabledelayedexpansion rem Solves a sudoku puzzle -- see help message for more info. set x=x%*& if "!x!"=="x" call :help& exit /b if exist %1 set x=x& for /f "delims=" %%r in (%1) do set x=!x!%%r set x=!x: =!& set x=!x:.=0! set y=!x!& for /l %%c in (0,1,9) do set y=!y:%%c=! if "!y!" neq "x" call :help :illegal board char(s): !y:~1!& exit /b set y=000000000& set x=%x:~1%!y!!y!!y!!y!!y!!y!!y!!y!!y! rem Below can create 100-200 env vars (depends on number of empty squares). rem Using these helper env vars enables the core logic to run much faster. for /l %%x in (0,1,8) do set x%%x=!x:~0,9!& set x=!x:~9! set /a xx=100433766, xy=221554887, $=-1 for /l %%r in (0,1,8) do for /l %%c in (0,1,8) do set %%r%%c=!x%%r:~%%c,1! set $$=1& for /l %%r in (8,-1,0) do for /l %%c in (8,-1,0) do ( if !%%r%%c!==0 if defined $$ ( set #%%r%%c=!$!^^!!xx:~%%r,1!!xx:~%%c,1!^^!^^!!xx:~%%r,1!!xy:~%%c,^ 1!^^!^^!!xy:~%%r,1!!xx:~%%c,1!^^!^^!!xy:~%%r,1!!xy:~%%c,1!^^! for /l %%i in (0,1,8) do ( if %%i neq %%c set #%%r%%c=!#%%r%%c!^^!%%r%%i^^! if %%i neq %%r set #%%r%%c=!#%%r%%c!^^!%%i%%c^^! ) set $=%%r%%c& set "$$=" for /f %%x in ("!#%%r%%c:~2!") do set x=%%x& for /l %%y in (1,1,9) do ( if "!x:%%y=!"=="!x!" set $$=!$$! %%y) set #%%r%%c=!#%%r%%c!!$$! ) ) if %$%==-1 call :help ~no empty board square(s) ***& exit /b if not defined $$ call :help ~invalid board ***& exit /b call :solve %$% || (call :help ~NO SOLUTION FOUND ***& exit /b) call :dispb & exit /b rem Solve all zeroes starting at %1= index of 1st zero currently in board. rem Returns errorlevel 0 if solution found, 1 if no solution. :solve if %1==-1 exit /b 0 set $=!#%1:~2,80! set +%1=%$% for %%i in (!#%1:~82!) do if "!+%1:%%i=!"=="!+%1!" ( set %1=%%i& call :solve !#%1:~0,2! && exit /b 0) set %1=0& exit /b 1 rem Display the 9x9 board :dispb (for /l %%r in (0,1,8) do (set "r=" for /l %%c in (0,1,8) do set r=!r!!%%r%%c!)& echo !r!)& exit /b rem Display Error/Help. If args, output them error msg first. :help set x=%*& if defined x echo %~nx0: *** !x:~1!& if "!x:~0,1!"=="~" exit /b echo.& echo %~nx0: 4/5/2022 version 1.1 http://paulhoule.com/phsud& echo. echo Solves sudoku puzzle using brute force, so very slow^^! echo Arg is puzzle: digits 1-9, 0 or . for empty square. echo Arg may be a filename; if so, puzzle is read from there. echo If puzzle is ^< 81 digits, trailing square(s) are assumed to be empty. echo.& echo Examples: echo %~n0 1023 -- 1,2,3 in top row indexes 0,2,3, all other squares 0 echo %~n0 brd -- board is read from file brd (brd must exist)

Click here to download  phsud.bat


Widget is loading comments...

You are visitor 1843       Go to Home Page