Skip to content

Instantly share code, notes, and snippets.

@mariokostelac
Created April 5, 2020 20:20
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 mariokostelac/6ca637b4939788ad33b52231fbf374d6 to your computer and use it in GitHub Desktop.
Save mariokostelac/6ca637b4939788ad33b52231fbf374d6 to your computer and use it in GitHub Desktop.
from constraint import *
def count_same_digits(n1, n2):
return len(set(n1).intersection(n2))
def count_same_position(n1, n2):
return (n1[0] == n2[0]) + (n1[1] == n2[1]) + (n1[2] == n2[2])
def check(n1, n2, correct_nums, correct_placed):
return count_same_digits(n1, n2) == correct_nums and count_same_position(n1, n2) == correct_placed
problem = Problem()
problem.addVariable("a", list(range(10)))
problem.addVariable("b", list(range(10)))
problem.addVariable("c", list(range(10)))
problem.addConstraint(lambda *num: check(num, [6,8,2], correct_nums=1, correct_placed=1), ("a", "b", "c"))
problem.addConstraint(lambda *num: check(num, [6,1,4], correct_nums=1, correct_placed=0), ("a", "b", "c"))
problem.addConstraint(lambda *num: check(num, [2,0,6], correct_nums=2, correct_placed=0), ("a", "b", "c"))
problem.addConstraint(lambda *num: check(num, [7,3,8], correct_nums=0, correct_placed=0), ("a", "b", "c"))
problem.addConstraint(lambda *num: check(num, [7,8,0], correct_nums=1, correct_placed=0), ("a", "b", "c"))
print(problem.getSolutions())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment