r/learnpython • • 2d ago

Does anyone play high level sudoku?

Hi,

I'm trying to make a very advanced sudoku player in python.

However, it generates sudokus far too slowly. Can someone help me? (I'll only share source code once someone says yes)

EDIT: found solution, deleting in a week

0 Upvotes

13 comments sorted by

12

u/SCD_minecraft 2d ago
  1. Don't ask to ask, just ask and post source code
  2. Sudoku is slow to create and solve, as it is basically graph coloring problem which has no fast known solutions

1

u/QueasyCarBoy 2d ago

i didnt think of the "dont ask to ask". I was just trying to prevent spam

1

u/POGtastic itertools evangelist 1d ago

Solving is actually pretty fast due to Sudoku being an exact cover problem, which can be solved with Knuth's Algorithm X and then optimized with the "Dancing Links" optimization (which is very gross to implement. Thanks, Donald).

For my purposes the regular ol' X, implemented naively, was fast enough and can solve Sudokus in about 150ms on my desktop computer from 2015.

Generation can be done with the same algorithm: if you start with a blank field and "solve" it while choosing rows of the constraint matrix randomly, the result is going to be a random Sudoku. Upon generating a Sudoku, you can then backtrack until you have a minimum of numbers while only allowing one valid solution.

Evaluating the difficulty of a generated Sudoku is probably harder.

6

u/schoolmonky 2d ago

Why would you refuse to show your code until someone agrees to help? That just slows the whole process down. If you show your code upfront, someone can jump right in with giving advice.

1

u/QueasyCarBoy 2d ago

Yeah i think i did this in the wrong context but i didnt want to spam ._.

2

u/LayotFctor 2d ago

How slow are we talking here?

1

u/QueasyCarBoy 2d ago

like "no visible progress in 30 entire seconds" slow.

1

u/LayotFctor 2d ago

Since sudoku states cannot be statically knowable to be solvable, you always need to run the solver to verify. Perhaps you forgot to discard an invalid board state and got stuck in an infinite loop?

Otherwise it's probably your algorithm, such as forgetting to check for the cell with the lowest number of options and creating an exponential complexity.

1

u/QueasyCarBoy 2d ago

edit: i think i got a solution, i'm deleting this in like a wek

1

u/Kevdog824_ 2d ago

Can you describe the steps you do to generate boards and verify solutions? We might be able to point out an issue with your approach

1

u/QueasyCarBoy 2d ago

sure, here is my best attempt to share my entire program: https://drive.google.com/file/d/1lURfk2XK25FNAq7oD_eU3TcQK4Mtz0H9/view?usp=sharing

1

u/Kevdog824_ 2d ago

I saw your comment below:

like "no visible progress in 30 entire seconds" slow.

The search runs on Tkinter’s main thread, so the GUI is blocked and can’t show progress or respond until generation finishes. That explains the "no visible progress", but not necessarily why generation takes so long. Which difficulty were you generating, and did this happen at startup or after clicking “New game”? That would help me reproduce the slowdown and investigate it.

0

u/[deleted] 2d ago

[deleted] — view deleted comment

1

u/QueasyCarBoy 2d ago

yeah that was what i was thinking. I was trying to make a program to play and generate really difficult sudokus (up to AIC difficulty), but it failed to generate anything past locked candidates difficulty.

And for up to naked singles, it just gave a sudoku with one missing number.