Skip to main content

Writing Sudoku Solvers

After writing a Nonogram solver, I decided to tackle a Sudoku solver to practice Rust. My goal wasn't just to support classic Sudoku rules, but also to handle variants like Thermometer, Arrow, and Cage etc.

1. Brute Force

It is fairly easy to write a brute force or backtracking algorithm. This approach is sufficient for most classic Sudoku puzzles, but it becomes unbearably slow as soon as variant rules are introduced.

I considered this step a warmup—a baseline to improve upon.

2. Constraint Propagation

Here, I tried to introduce "logical thinking" to the algorithm. I used u16 as a bitmask to represent the possible values of a cell. Whenever a cell's state changes (due to guessing, backtracking, or propagation), the algorithm consults all constraints to eliminate impossible candidates.

While Nonogram is technically an NP-Complete problem, in practice, my constraint-propagation solver (without guessing) can solve almost all puzzles found online. I’ve only seen one exception where I had to guess a few cells. This proves that puzzles designed for human players are meant to be solved via logical deduction, making them computationally "easy."

It turns out Sudoku is similar. Although some backtracking is still needed, classic puzzles are typically solved within 100~200 µs (microseconds), while variants might take a few milliseconds. I did see one puzzle take ~20 seconds, but overall, I was happy with the result.

So, can it go faster? Two optimization options came to mind:

  1. When a cell changes, only consult relevant constraints rather than re-checking all of them.
  2. Instead of copying the entire board state for every guess, I could carefully track changes and undo them during backtracking. However, since the board state is already quite small, I doubted this would yield a significant performance boost in practice.

Right before I decided to optimize further, I learned something new.

3. Dancing Links (DLX)

I discovered "Dancing Links" while asking an AI for the best Sudoku algorithms. This is a technique invented by Donald Knuth to efficiently implement Algorithm X, which solves the "exact cover" problem.

This is perfect for classic Sudoku, where the goal is to find an exact cover between "putting a digit in a cell" and "satisfying every row, column, and box constraint."

This is perfect for the classic Sudoku algorithm, where we essentially try to find the exact cover between "putting a digit into each cell" and "each row/column/box must contain exactly one digit X (where X is 1 ~ 9)".

The magical part: We can precisely undo this process by restoring the covered rows and columns. This means we don't need to copy the state during backtracking! The algorithm only needs to remember the latest guess; the data structure itself holds the information required to reverse it.

However, there's a catch: things get complicated quickly with variant rules. Only a few rules (like distinct values) can be easily encoded into the DLX structure. For most others, I had to implement them as "external observers" that eliminate impossible candidates after a guess. This forced me to maintain an undo stack again, essentially plugging a constraint propagation engine back into DLX.

Another issue: DLX wasn't actually faster than my custom constraint propagation solver. Since it consistently took ~200 µs for classic puzzles, I didn't bother implementing the complex variant rules for it.

4. Generic Solvers

I talked to a colleague about my progress, and he asked, "Why are you writing a custom solver? Why not just use a generic SAT/SMT solver like Z3?"

That was a good point. I did some quick research and picked three candidates to test:

The results are shown below.

5. Results

Here is how the solvers compared. (cp = my constraint propagation implementation, dlx = my dancing links implementation)

Classic Sudoku 1 (Easy)

  • cp: ~100 µs
  • dlx* ~200 µs
  • OR-Tools: ~10 ms
  • Z3: ~20 ms
  • cvc5: ~70 ms

Classic Sudoku 2 (Harder)

  • cp: ~200 µs
  • dlx: ~200 µs
  • OR-Tools: ~10 ms
  • Z3: ~80 ms
  • cvc5: ~200 ms

Hard: Empty Board + Thermometer Rules

  • cp: ~3 ms
  • OR-Tools: ~30 ms
  • Z3: ~20 s
  • cvc5: ~23 s

Hard: Almost Empty Board + Arrow Rules

  • OR-Tools: ~200 ms (slightly faster than cp)
  • cp: ~200 ms
  • Z3: ~20 s
  • cvc5: ~100 s

6. Discussion

I found these results both surprising and reasonable.

OR-Tools is significantly faster than Z3 and cvc5. I believe this is because OR-Tools uses a CP-SAT (Constraint Programming) solver, whereas Z3 and cvc5 are primarily SMT (Satisfiability Modulo Theories) solvers. Since the Sudoku puzzles I used are designed for human logic, they align better with the constraint propagation techniques used by OR-Tools.

My custom solver vs. OR-Tools. For easy puzzles, OR-Tools is slower than my implementation. This is likely due to initialization overhead; my code is hyper-optimized specifically for Sudoku. However, OR-Tools catches up quickly on complex puzzles. My constraint propagation logic is naturally inferior to the sophisticated heuristics inside OR-Tools, so as complexity rises, the generic solver wins.

7. Conclusion

I had a lot of fun and learned a great deal during this process. My next step is to explore OR-Tools further. Perhaps I'll write solvers for even more complex puzzles without reinventing the wheel!

Comments

Popular posts from this blog

A Rocky Migration: Moving from docker-compose to Podman and gVisor

I've been running a few containers for several years. They were all running under rootless Docker with a single user. Initially, I planned to  migrate the containers to VMs , but I couldn't get a stable workflow after about two months of effort. Later,  gVisor caught my attention , and I decided to migrate to Podman with gVisor instead. The new plan is to run each container with  --userns=auto  and use Quadlet for systemd integration. This approach provides better isolation and makes writing firewall rules easier. I'm now close to migrating all my containers. Here are a couple of rough edges I'd like to share. Network Layout I compared  various networking options  and spent a few hours trying the one-interface-per-group approach before giving up. I settled on a single macvlan network and decided to use static IP addresses for my containers. To prevent a randomly assigned IP address from conflicting with a predefined one, I allocated a large IP range for my ...

GameConqueror 0.09 -- Linux Game Hacking Tool

If you are a game hacker If you've been looking for a `CheatEngine for Linux` Then you can't miss this. ============================================== GameConqueror is a game hacking tool for linux, it's written in PyGTK and uses scanmem as its backend. It's supposed to be with most useful features of CheatEngine for Linux. Currently, I've implemented almost everything about scanning, involving variant data types and scan types: Data Types: int{8/16/32/64}, float{32/64}, unknown type(int or float) and unknown width(will try each of them), byte array and string Scan Types: equal, greater, less, changed, unchanged, increased(by), decreased(by) This should be enough for most cases, so I decided to release it at the current status. ============================================= Here's how you can get it PPA (for Ubuntu users) https://launchpad.net/~coolwanglu/+archive/scanmem (I've not test it in 32bit environments or Jaunty, do please inform me if it doe...

Fix Google Security Code

Google Security Code (http://g.co/sc) is one type of 2-step verification. This is particularly useful when security keys and passkeys are not available. I have been using it in my LXC containers, until today I found out that it stopped working. It just kept saying "The code is invalid". It is easy to rule out some factors: The code works on other browsers on my laptop. The code works on other devices that are directly connected to the router. So it appears that Google also checks IP addresses besides the security code. Recently I have IPv6 enabled, so most devices that are directly connected to the router have both IPv4 and IPv6 addresses. But  I only enabled IPv4 for my LXC containers. So I guess when a code is generated by device A and used by device B, Google should be able to check that device A and device B are closely located. But in my case, IPv6 address appears on device A but not on device B, which may look suspicious. To fix the problem, I just needed to disable IPv...