/* Family riddle in Picat. From https://rulesolver.wordpress.com/sample-decision-model/family-riddle/ """ Problem Description. This problem is offered as a DMCommunity’s challenge Jan-2024. Let’s assume that Rene and Leo are both heads of household, and, what a coincidence, both families include three girls and three boys. The youngest child in Leo’s family is a girl, and in Rene’s family, a little girl has just arrived. In other words, there is a girl in Rene’s family whose age is less than one year. Neither family includes any twins, nor any children closer in age than a year. All the children are under age ten. In each family, the sum of the ages of the girls is equal to the sum of the ages of the boys; in fact, the sum of the squares of the ages of the girls is equal to the sum of the squares of the ages of the boys. The sum of the ages of all these children is 60. Question: What are the ages of the children in these two families? How many solutions has this problem? """ There is one solution with symmetry breaking (ordered ages): [symmetryBreaking = true,print = true] leo = [girls = [3,7,8],boys = [4,5,9]] rene = [girls = [0,5,7],boys = [1,3,8]] count = 1 There are 144 different solutions (w/o symmetry breaking): [symmetryBreaking = false,print = false] count = 144 A write up of this model is available as a PDF: http://hakank.org/picat/FamilyRiddle.pdf Cf family_riddle.pi which is the same puzzle but slightly different wordings and objective of the problem. This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go ?=> member([SymmetryBreaking,Print], [[true,true], [false,false] ]), println([symmetryBreaking=SymmetryBreaking,print=Print]), Count = count_all(family_riddle(SymmetryBreaking,Print)), println(count=Count), nl, fail, nl. go => true. family_riddle(SymmetryBreaking,Print) => MaxAge = 9, N = 3, % Leo's children LeoBoys = new_list(N), LeoBoys :: 0..MaxAge, LeoGirls = new_list(N), LeoGirls :: 0..MaxAge, Leos = LeoBoys ++ LeoGirls, % Rene's children ReneBoys = new_list(N), ReneBoys :: 0..MaxAge, ReneGirls = new_list(N), ReneGirls :: 0..MaxAge, Renes = ReneBoys ++ ReneGirls, % All different ages (in each family) all_different(Leos), all_different(Renes), % Youngest is a girl ReneGirls[1] #= min(Renes), LeoGirls[1] #= min(Leos), % Rene has a newborn girl ReneGirls[1] #= 0, % Sums sum(ReneBoys) #= sum(ReneGirls), sum(LeoBoys) #= sum(LeoGirls), % Sum of squares sum([ReneBoys[I]**2 : I in 1..N]) #= sum([ReneGirls[I]**2 : I in 1..N]), sum([LeoBoys[I]**2 : I in 1..N]) #= sum([LeoGirls[I]**2 : I in 1..N]), Vars = Leos ++ Renes, % Sum of all is 60 sum(Vars) #= 60, % Symmetry breaking if SymmetryBreaking then increasing(LeoBoys), increasing(LeoGirls), increasing(ReneBoys), increasing(ReneGirls) end, solve(Vars), if Print then println('leo '=[girls=LeoGirls,boys=LeoBoys]), println(rene=[girls=ReneGirls,boys=ReneBoys]) end.