import constraint


problem = constraint.Problem()

nationality = ["English", "Spanish", "Ukrainian", "Norwegian", "Japanese"]
pet = ["dog", "snails", "fox", "horse", "zebra"]
cigarette = [
    "Old Gold", "Kools", "Chesterfields", "Lucky Strike", "Parliaments"
]
colour = ["red", "green", "yellow", "blue", "ivory"]
beverage = ["coffee", "milk", "orange juice", "water", "tea"]

criteria = nationality + pet + cigarette + colour + beverage
problem.addVariables(criteria, [1, 2, 3, 4, 5])

problem.addConstraint(constraint.AllDifferentConstraint(), nationality)
problem.addConstraint(constraint.AllDifferentConstraint(), pet)
problem.addConstraint(constraint.AllDifferentConstraint(), cigarette)
problem.addConstraint(constraint.AllDifferentConstraint(), colour)
problem.addConstraint(constraint.AllDifferentConstraint(), beverage)

problem.addConstraint(lambda e, r: e == r, ["English", "red"])
problem.addConstraint(lambda s, d: s == d, ("Spanish", "dog"))
problem.addConstraint(lambda c, g: c == g, ("coffee", "green"))
problem.addConstraint(lambda u, t: u == t, ("Ukrainian", "tea"))
problem.addConstraint(lambda g, i: g - i == 1, ("green", "ivory"))
problem.addConstraint(lambda o, s: o == s, ("Old Gold", "snails"))
problem.addConstraint(lambda k, y: k == y, ("Kools", "yellow"))
problem.addConstraint(constraint.InSetConstraint([3]), ["milk"])
problem.addConstraint(constraint.InSetConstraint([1]), ["Norwegian"])
problem.addConstraint(lambda c, f: av bs(c - f) == 1, ("Chesterfields", "fox"))
problem.addConstraint(lambda k, h: abs(k - h) == 1, ("Kools", "horse"))
problem.addConstraint(lambda q, o: q == o, ["Lucky Strike", "orange juice"])
problem.addConstraint(lambda j, p: j == p, ["Japanese", "Parliaments"])
problem.addConstraint(lambda k, h: abs(k - h) == 1, ("Norwegian", "blue"))

solution = problem.getSolutions()[0]

for i in range(1, 6):
    for x in solution:
        if solution[x] == i:
            print(str(i), x)
