1 Star 0 Fork 0

Awu-Miao/learn-z3

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
4-match-assignments.py 750 Bytes
一键复制 编辑 原始数据 按行查看 历史
PwnFunction 提交于 2022-07-13 12:03 . updated README.md
#!/usr/bin/env python3
import z3
"""
There is an assignment of unique digits to letters such that the equation `send + more = money` holds.
Find an assignment of the digits to make this true.
"""
digits = z3.Ints('o d n m e y s r')
o, d, n, m, e, y, s, r = digits
def join_numbers(l):
return z3.Sum([10**i * v for i, v in enumerate(l[::-1])])
send = join_numbers([s, e, n, d])
more = join_numbers([m, o, r, e])
money = join_numbers([m, o, n, e, y])
solver = z3.Solver()
solver.add(z3.Distinct(digits))
solver.add(send + more == money)
solver.add([z3.And(i >= 0, i <= 9) for i in digits])
solver.add([s > 0, m > 0])
if solver.check() == z3.sat:
m = solver.model()
print(f'{m}\n{m.eval(send)} + {m.eval(more)} = {m.eval(money)}')
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/Awu-Miao/learn-z3.git
[email protected]:Awu-Miao/learn-z3.git
Awu-Miao
learn-z3
learn-z3
main

搜索帮助