/* Sack of colored balls in Picat. From Presh Talkwalkar (MindYourDecisions) """ A sack contains 4 different colored balls of which 14 balls are not blue, 16 balls are not yellow, 24 balls are not red, and 12 balls are not pink. How many balls are in the sack? """ Solution: There is no solution to this problem!' If we allow negative number of balls then there is a unique solution: n = 22 balls = [8,6,-2,10] I.e. negative 2 red balls. (My previous version of this model was wrong since it had a typo Pink->Pint.) This model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go ?=> Balls = [Blue,Yellow,Red,Pink], Balls :: 0..100, % Balls :: -10..100, % Uncomment this for a solution with -2 red balls N #= sum(Balls), 14 #= sum([ C*(C #!= Blue) : C in Balls ]), 16 #= sum([ C*(C #!= Yellow) : C in Balls ]), 24 #= sum([ C*(C #!= Red) : C in Balls ]), 12 #= sum([ C*(C #!= Pink) : C in Balls ]), Vars = Balls ++ [N], solve(Vars), println(n=N), println(balls=Balls), nl, fail, nl. go => true. % Simpler approach. This time with an impossible domain: % % balls = [blue = 8,yellow = 6,red = -2,pink = 10] % n = 22 go2 => Balls = [Blue,Yellow,Red,Pink], % Balls :: 1..100, Balls :: -10..100, N #= sum(Balls), N - Blue #= 14, N - Yellow #= 16, N - Red #= 24, N - Pink #= 12, solve(Balls ++ [N]), writeln(balls=[blue=Blue,yellow=Yellow,red=Red,pink=Pink]), writeln(n=N), nl, fail.