# -*- Mode: Python -*- from constraint import * # http://brownbuffalo.sourceforge.net/index.html # The Dillies have five teenaged children, two boys named Ollie and # Rollie, and three girls named Mellie, Nellie, and Pollie. Each is a # different number of years old, from 13 to 17. There are three # bedrooms for the children in the Dillie house, so two share the # yellow room, two share the white room, and one alone has the smaller # green room. Can you match each one's name and age, and tell who # sleeps where? # # 1. No one shares a room with a sibling of the opposite sex. # 2. Pollie is exactly one year older than Mellie. # 3. The two teenagers who share the yellow room are two years apart in age. # 4. The two who share the white room are three years apart in age. # 5. Rollie is somewhat older than Ollie, but somewhat younger than the sibling who has the green room. # # Determine: Child -- Age -- Room p = Problem() map = { 'O': 'Ollie', 'R': 'Rollie', 'M': 'Mellie', 'N': 'Nellie', 'P': 'Pollie', 'y0': 'yellow room 0', 'y1': 'yellow room 1', 'w0': 'white room 0', 'w1': 'white room 1', 'g': 'green room', } order = [13,14,15,16,17] p.addVariables (map.keys(), order) p.addConstraint (AllDifferentConstraint(), "ORMNP") p.addConstraint (AllDifferentConstraint(), ('y0','y1','w0','w1','g')) pa = p.addConstraint pa (lambda y0,y1,O,R,M,N,P: (y0 in (O,R) and y1 in (O,R) and y0 != y1) or (y0 in (M,N,P) and y1 in (M,N,P) and (y0 != y1)), ('y0', 'y1', 'O','R','M','N','P')) pa (lambda P,M: P == M+1, ('P','M')) pa (lambda y0,y1: (y0 == y1+2) or (y1 == y0+2), ("y0","y1")) pa (lambda w0,w1: (w0 == w1+3) or (w1 == w0+3), ("w0","w1")) pa (lambda R,O,g: (R > O and R < g), ('R','O','g')) # unique solutions pa (lambda y0,y1: y0 < y1, ('y0', 'y1')) pa (lambda w0,w1: w0 < w1, ('w0', 'w1')) solution = p.getSolutions()[0] solution = [(v,k) for (k,v) in solution.items()] solution.sort() for i in range (5): print i, for j in range (2): v,k = solution.pop(0) print v,map[k] + ',', print