@@ -60,12 +60,13 @@ Some common z3 patterns that use comprehensions include:
A common constraint that appears in puzzles is that a set of cells must be filled with numbers so that no two cells contain the same number.
It is possible to create such a constraint by asserting inequality for every pair of cells.
However, since this is such a common pattern, z3 actually provides a function to assert that each variable must take on a *Distinct* value. Let's look at this in the context of cryptarithms:
However, since this is such a common pattern, z3 actually provides a function to assert that each variable must take on a *distinct* value. Let's look at this in the context of a [cryptarithm](https://en.wikipedia.org/wiki/Verbal_arithmetic):
```python
# Solve the cryptarithm
# SEND + MORE = MONEY
# z3 dict pattern: Dict[str, ArithRef]
letters={c:Int(c)forcinset('SENDMOREMONEY')}
s=Solver()
...
...
@@ -231,12 +232,14 @@ else:
print("No solution")
```
You can find the complete code in [sudoku.py](sudoku.py).
NOTE: I know that there are some occurences of repetition throughout this code.
I wrote it this way to intentionally "separate" each "type" of condition from each other.
If I had bugs in this, it is much easier to debug and tweak conditions when they are processed in separate blocks of code.
Personally, a bit more boilerplate is much more preferable than code that is a pain to debug.
### Modifying Your Solver
### Modifying The Solver
Now that we've written our solver, it's possible to *modify* it to solve sudoku variants as well (something you can't do with online solvers)!
For example, Killer Sudoku has the additional constraint that some regions of cells must add up to a particular number.
...
...
@@ -277,4 +280,7 @@ This behavior is different from `cond == expr`.
## Pseudo Boolean Constraints
<!-- TODO -->
\ No newline at end of file
<!-- TODO -->
Phew! These were long notes, but with that, we've covered the relevant basic z3 features that we'll be using!
Future notes will focus on *representation*, or how we can use these base features to represent higher-level concepts.