Sudoku Solver – 2nd Session

This is just a stream of thought blog post as I’m coding a Sudoku solving program.  I’m a beginner coder, so the code won’t be the best, but I’m learning as I go.

Click here to see the first post

Last thing I said the the last post was to use 3 digits to reference the cell. A short thought later and I’m thinking 4 would be easier.

[column][row][cell number in sub-grid][sub-grid]

Why 4?

  • I can query a row by looping through the column number
  • I can query a column by looping through the row number
  • I can check for a unique entry in a single sub-grid
  • Cell number will come in to play when solving more complex puzzles

Sounds simple right?  I’m writing this after I’ve done the work.

There are 2 ways to do this. A straight up 9×9 grid.  The row and column numbers are easy.  Adding in a sub-grid number and a cell number would make it a bit harder.

I ended up working out the logic by working on the smallest component, a cell, then building a 3×3 sub-grid (calling the cell function for each cell), then finally building the entirety of the grid.

Below is what I ended up coding.  I know they say good code can be read well and doesn’t need comments. Below has a comment for almost every line.

Produces this output:

 

Next ‘Session’ – Entering a puzzle to be solved.

Leave a Reply