Rudy’s OBTF Rudolf Adamkovič

Home / Computer science / Zebra puzzle / Computation


Solution

Who drinks water?

  1. \(i\)-th house such that \(x_{2, i, 5} = 1\)
  2. \(j\)-th nation such that \(x_{5, i, j} = 1\)

Who owns the zebra?

  • \(i\)-th house such that \(x_{1, i, 5} = 1\)
  • \(j\)-th nation such that \(x_{5, i, j} = 1\)
class SolutionPrinter(CpSolverSolutionCallback):
    def __init__(self):
        CpSolverSolutionCallback.__init__(self)
        self.__solution_count = 0

    def __solution(self, attribute, value):
        i = next(i for i in range(m) if self.value(x[attribute, i, value]))
        j = next(j for j in range(n) if self.value(x[Attribute.NATION, i, j]))
        return Nation(j).name

    def on_solution_callback(self) -> None:
        self.__solution_count += 1
        w = self.__solution(Attribute.DRINK, Drink.WATER)
        z = self.__solution(Attribute.ANIMAL, Animal.ZEBRA)
        print("Solution #%d" % self.__solution_count)
        print("Who drinks water? %s." % w)
        print("Who owns the zebra? %s." % z)

solver = CpSolver()
solver.SearchForAllSolutions(model, SolutionPrinter())
Solution #1
Who drinks water? NORWEGIAN.
Who owns the zebra? JAPANESE.

© 2025 Rudolf Adamkovič under GNU General Public License version 3.
Made with Emacs and secret alien technologies of yesteryear.