Created
April 29, 2019 22:50
-
-
Save gabrielsanchez/d7bc9c1c87b604d16872739323f0553a to your computer and use it in GitHub Desktop.
send more money CSP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#send more money | |
#gabriel sanchez | |
#github.com/gabrielsanchez | |
from constraint import * | |
p = Problem() | |
d = range(0,10) | |
p.addVariable("s", d) | |
p.addVariable("e", d) | |
p.addVariable("n", d) | |
p.addVariable("d", d) | |
p.addVariable("m", d) | |
p.addVariable("o", d) | |
p.addVariable("r", d) | |
p.addVariable("y", d) | |
p.addConstraint(AllDifferentConstraint(),('s','e','n','d','m','o','r','y')) | |
p.addConstraint(lambda m: m>0,('m')) | |
p.addConstraint( | |
lambda s,e,n,d,m,o,r,y: | |
1000*(s+m) + 100*(e+o) + 10*(n+r) + d+e == 10000*m + 1000*o + 100*n +10*e+y, | |
('s','e','n','d','m','o','r','y') | |
) | |
solution = p.getSolution() | |
print(solution) | |
# {'m': 1, 'd': 7, 'e': 5, 'n': 6, 'o': 0, 'r': 8, 's': 9, 'y': 2} | |
# 9567 | |
#+ 1085 = | |
# 10652 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment