CodingTrain / Suggestion-Box

A repo to track ideas for topics
572 stars 86 forks source link

Sudoku #911

Open tiredoftry opened 6 years ago

tiredoftry commented 6 years ago

Sudoku offers several things from other coding challenges combined with some additional logic.

Base rules for a classic game are..      9 x 9 grid filled with 3 x 3 grids containing the numbers 1 - 9      numbers 1 - 9 cannot repeat in any 3 x 3 grid      numbers 1 - 9 cannot repeat in any 1 x 9 row      numbers 1 - 9 cannot repeat in any 1 x 9 column

Base challenge could be to...      create the 9 x 9 and 3 x 3 grids      prefill some numbers to create an easy, medium and hard puzzle

barongello commented 6 years ago

1 x 9 row and 9 x 1 column, no?

tiredoftry commented 6 years ago

You are correct, let me fix that

JohnyDL commented 6 years ago

I went away and coded the basic rules onto Khan Academy for myself cause it looked like an interesting challenge and the computer only knowing the numbers 1 to 9 must be in every row column or block isn't actually enough, I got a fair number of easy and medium difficulty sudokus solved but harder ones won't

My approach was define the grid and let each square be able to 'see' their neighbors and keep a track of the number that square could be So 81 cells with arrays [1,2,3,4,5,6,7,8,9] for possible values for the square, and a neighbours array [[other cells in my row][other cells in my column][other cells in my block]] Then the process was to tell each cell if they can see one of their neighbours is definitely a value to remove that value from their possibilities (I called this solve mode 1) I've since added the ability to spot constraints where 2 or 3 cells share 2 groups and in both groups they're the only one to contain a given value, so looking across a row another block might contain 2 cells in this row that have the possibility of a 5, but no other cells in that block have that possibility so since the 5 must be in those cells the originating cell for the check cannot contain a 5, it's a longer chain of logic for the computer (and I called that solve mode 2)

I'm still working on additional solve modes and if you're interested you might be able to follow some of the code or offer help: https://www.khanacademy.org/computer-programming/starter-sudoku-solver/4921068897206272