Skip to content

Instantly share code, notes, and snippets.

@Saren-Arterius
Last active August 13, 2016 19:33
Show Gist options
  • Save Saren-Arterius/798cefdb2c33e0eb4474be273c4ffea1 to your computer and use it in GitHub Desktop.
Save Saren-Arterius/798cefdb2c33e0eb4474be273c4ffea1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
# Problem: http://9gag.com/gag/a6Qq368?ref=fb.s
# sudo pip2 install python-constraint
from constraint import *
variables = "a", "b", "c"
problem = Problem()
for v in variables:
problem.addVariable(v, range(10))
problem.addConstraint(AllDifferentConstraint())
problem.addConstraint(FunctionConstraint(lambda a, b, c: (a == 5) + (b == 4) + (c == 8) == 1), variables)
problem.addConstraint(NotInSetConstraint([5, 3, 0]))
problem.addConstraint(FunctionConstraint(lambda a, b, c: (a in [5, 7]) + (b in [1, 7]) + (c in [1, 5]) == 2, variables))
problem.addConstraint(FunctionConstraint(lambda a, b, c: (a in [0, 6]) + (b in [8, 6]) + (c in [8, 0]) == 1, variables))
problem.addConstraint(FunctionConstraint(lambda a, b, c: (a in [4, 7]) + (b in [6, 7]) + (c in [6, 4]) == 1, variables))
print(problem.getSolutions())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment