from z3 import * a = Int('a') b = Int('b') c = Int('c') d = Int('d') s = Solver() s.add(b == a - 2) s.add(d == c + 2) s.add(d == a + 5) s.add(b - 20 + c - 20 == 15) if s.check() == sat: print(s.model()) # _solve = s.model() # print(_solve)
s = Solver() code = [Int(str(i)) for i inrange(3)]
defGoodValueBadPlace(nums, count): exps = [] for i inrange(len(nums)): for j inrange(len(code)): if i == j: continue else: exps.append(If(nums[i] == code[j], 1, 0)) return Sum(exps) == count
# one number is correct and well-placed s.add(Or(code[0] == 2, code[1] == 9, code[2] == 1)) # one number is correct but wrong placed s.add(GoodValueBadPlace([2, 4, 5], 1)) # two numbers are correct but wrong placed s.add(GoodValueBadPlace([4, 6, 3], 2)) # nothing is correct s.add(And(code[0] != 5, code[1] != 7, code[2] != 9)) # one number is correct but wrong placed s.add(GoodValueBadPlace([5, 6, 9], 1))
if s.check() == sat: m = s.model() for c in code: print(m[c], end=' ')