from constraint import *
"""
Python version of the Seseman's Convent Problem

See further
"Sesemans matematiska klosterproblem samt lite Constraint Logic Programming"
http://www.hakank.org/webblogg/archives/001084.html


A B C
D _ E
F G H

where 
A+B+C = T
A+D+F = T
C+E+H = T
F+G+H = T

and where
 A+B+C+D+E+F+G+H = Sum

In the Seseman's Convent problem:
T = 9
Sum = {24, 28, 20, 32}

"""

def main():
    problem = Problem()
    problem.addVariables("abcdefgh", range(1,9))
    problem.addConstraint(lambda a, b, c: a + b + c== 9, "abc")
    problem.addConstraint(lambda a, d, f: a + d + f== 9, "adf")
    problem.addConstraint(lambda c, e, h: c + e + h== 9, "ceh")
    problem.addConstraint(lambda f, g, h: f + g + h== 9, "fgh")
    problem.addConstraint(lambda a,b,c,d,e,f,g,h: a + b + c + d + e + f + g + h == 24, "abcdefgh")        

    c = 0;
    for s in problem.getSolutions():
        c = c + 1
        print "%(a)d %(b)d %(c)d\n" \
              "%(d)d _ %(e)d\n" \
              "%(f)d %(g)d %(h)d\n" % s

    print c, "solutions"
    
if __name__ == "__main__":
    main()
