Skip to content

Instantly share code, notes, and snippets.

@jan-g
Last active July 10, 2019 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jan-g/0dd924a002a673851671349b3a70fa3c to your computer and use it in GitHub Desktop.
Save jan-g/0dd924a002a673851671349b3a70fa3c to your computer and use it in GitHub Desktop.
# pip install z3-solver
from z3 import *
s = Solver()
w = Int('w')
m = Int('m')
s.add(0 <= m, m <= 12)
s.add(0 <= w, w <= 12 - m)
s.add(m / Q(2,1) + w / Q(4,1) + 2 * (12 - m - w) == 12) # Q(a,b) is the rational a/b
while True:
if s.check() != sat:
break
mod = s.model()
print(mod)
s.push()
# Add a constraint meaning "find a solution that doesn't include this one"
s.add(w != mod[w], m != mod[m])
# output:
[w = 6, m = 1]
[w = 0, m = 8]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment