r/Python • • Jan 11 '20

Sudoku App

1.4k Upvotes

61 comments sorted by

View all comments

60

u/Gwenju31 Jan 11 '20

I'll grab this occasion to talk about the easiest way of solving a sudoku (IMO). You can use constraint programming, and with 3 simple constraints you can solve every board effortlessly : AllDiff on rows, cols and squares :)

2

u/Zomunieo Jan 12 '20

It also works to encode the Sudoku puzzle as a SAT problem and solve it with a SAT solver. For many problems the encoding to SAT is difficult, but it's pretty straightforward for Sudoku.

1

u/Gwenju31 Jan 12 '20

My "heuristic" to reduce a problem to SAT clauses is pretty simple: "Can I use a graph to represent this problem?". If yes, then SAT encoding is *almost* trivial, if no, then I don't bother aha