Scientific Calculator

(public) vasilkadifeli/SolveSudoku

By vasilkadifeli Vasil Kadifeli

Some silly recursive code to solve your Sudoku puzzles for all possible solutions

Tagged: printout recursion sudoku

// Some silly recursive code to solve your Sudoku puzzles
// You will receive all possibles solution confind by your computer's cpu strength
// but you can control the number of solutions you want to get 
// Call as : 
//    var printout = SolveSudoku(SN)
// where SN == 0 for all solutions or SN == n for just the first n solutions
// then go to the variables window and look at var printout
// sample sudoku data with 2 possible solutions
// 400000389/009703000/605002001/000400010/890001620/020800004/000000040/200150006/706200508/

var SolveSudoku = function(SN){

  var printout=[]; 
  var printline="";
  var solnum = 0;
  var tab = zeros(9,9);
  var str=""; cell =""; 
  var chr=0; row=0; col=0; num=0;
  var abort=false;


  var PrintSolution = function(){

    var i=0; j=0;

    solnum = solnum +1;
    push(printout,concat("Solution no:",string(solnum)));
    push(printout," ");
    i=0; 
    while (i < 9) ->
      ( j=0;
        printline = "";
        while (j < 9) ->
          ( printline = concat(printline,string(tab[i][j]));
            (j+1) mod 3 == 0 ? printline = concat(printline," ") :0;
            j=j+1;
          );
        push(printout,printline);
        (i+1) mod 3 == 0 ? push(printout," ") :0;
        i=i+1;
      ); 
    push(printout," ");

    SN > 0 ? 
      ( solnum == SN ? abort=true :0; )
    : ( solnum mod 10 == 0 ? abort = confirm(concat("So far ",string(solnum),
                                                    " solutions found, abort ?"),
                                             "Abort YES/NO?") :0; );

  }; // end PrintSolution


  var SolveTable = function(pos){

    var i=0; j=0; ii=0; jj=0; r=0; c=0; val=0; inv=false;  

    not abort ?
      ( pos > 81 ? 
          ( PrintSolution() ) 
        : ( r = ipart((pos-1)/9);
            c = (pos-1) - r*9; 
            tab[r][c] == 0 ?
              ( val = 1;
                while (val <= 9) ->
                  ( inv=false;
                    i=0;
                    while (i < 9 and not inv) -> 
                      ( tab[r][i] == val or tab[i][c] == val ? inv=true :0;
                        i=i+1 );
                    ii = ipart(r/3)*3;
                    i = ii;
                    while (i < ii+3 and not inv) ->
                      ( jj = ipart(c/3)*3;
                        j = jj;
                        while (j < jj+3 and not inv) ->
                          ( tab[i][j] == val ? inv=true :0;
                            j=j+1 );
                        i=i+1 );
                    not inv ? 
                      ( tab[r][c] = val;
                        SolveTable(pos+1) ) :0; 
                    val=val+1;
                  );
                tab[r][c] = 0; // backtrack            
              )
              : SolveTable(pos+1);
          );
      )
      : 0;
  }; // end SolveTable

  

  // main program
  
  number(SN) ? SN=number(SN) : SN=0;

  str=prompt(concat("Enter 81 digits left to right, top to botom. ",
                    "Place a zero for an empty position ",
                    "and insert a / for each end of line."), "Sudoku Initial Position");


  len str != 90 ? error("Data must be 90 chars long.") :0;  
  chr=0; row=0; col=0;
  while (chr < 90) ->
    ( cell = slice(str,chr,chr+1);
      cell == "/" ?
        (chr+1) mod 10 != 0 ? 
           error(concat("Character / at wrong position :",string(chr+1))) 
        : 0
      : ( cell == "0" ? num=0 
          : number(cell) ? num=number(cell) 
            : error(concat("Number expected at position :",string(chr+1)));
          tab[row][col] = num;
          col == 8 ? (row=row+1; col=0;) : col=col+1;     
         );
      chr=chr+1;
    );

    SolveTable(1);
    printout


}; // end SolveSudoku

spam? | offensive?

2 Comments

Sign in to leave a comment