Don't know where to start in Sudoku Solver

So I really don’t get how or what’s the point of it lol…

So the first story says:

You can POST /api/solve with form data containing puzzle which will be a string containing a combination of numbers (1-9) and periods . to represent empty spaces. The returned object will contain a solution property with the solved puzzle.

Ok sounds simple enough but where do I get the solution for the initially loaded puzzle ? I looked at the puzzlesAnd Solutions array and the puzzle doesn’t correspond to any of the puzzles in array! Am I expected to solve the initial puzzle by myself? Where do I get the solution for the initial puzzle?

Not specifically solve it by yourself, but write logic that’s capable of solving valid sudoku puzzle.

1 Like

@synerjay I would try iterating over quick wins and incremental bruteforcing of the game tree.

1 Like

So I already made functions for checking Rows Columns and Regions…

Now I’m working on the Solve function and it seems impossible!

This is my code so far:

`for (i = 0; i < mergedArray.length; i++) {

    let [ row, column ] = mergedArray[i][1].split('');
    let value = mergedArray[i][0];

    if (value === '.') {

      let num = 1

      while (num < 10) {
        if (solver.checkRowPlacement(puzzleString, row, column, num.toString()) === 'valid' && solver.checkColPlacement(puzzleString, row, column, num.toString()) === 'valid' && solver.checkRegionPlacement(puzzleString, row, column, num.toString()) === 'valid' ) {
            puzzleArray[i] = num.toString()
            puzzleString = puzzleArray.join('')
        }
        num++
      }
    }

  }
  console.log(puzzleString);
  res.json({solution: puzzleString});`

So far this code can solve the first 14 numbers but it makes a mistake at B5 where number 7 and number 9 are both valid numbers! But my code mistakenly puts 7 as its answer since it is the first number in the iteration to have valid in ALL conditions but in the solution it’s actually 9! This seems impossible to code! Can anyone help me out? I’m losing my mind lol

That’s part of the problem and it can be tricky. At this point both possibilities seems to be valid, but later the wrong one (or rather more than one, as that situation is bound to be encountered multiple times) will arrive at the point where it will not be possible to complete puzzle. That will show that, at one of the previous cells, where more than one number could have been placed, it was placed not the right one.

Recursion and backtracking and algorithms. Oh my !!!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.