/* Before and After puzzle in Picat. http://l4f.cecs.anu.edu.au/puzzles/expert/before-and-after """ OK, same four kids, different jellybeans. At the start of this exciting adventure, they each have a different number and all the numbers are greater than 1 and less than 10. David gives some (one or more) of his beans to Alice, and Claire gives some but not all of hers to Boris. Then Boris gives some to Alice (lucky Alice!) and David gives some to Claire. All of the numbers of beans given in these transfers are different, and each is less than 7. As a result of all that, they again each have a different number, and again all the numbers are greater than 1 and less than 10. No child has the same number of beans after as any child (themselves included) did before. At the end, Alice has one more than David and Claire has one less than Boris. Alice gets more from David than she does from Boris. How many does each of them have, before and after? """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go ?=> L = [Alice,Boris,Claire,David], L = 1..4, N = 4, NumSteps = 3, NumGifts = 4, % decision variables X = new_array(NumSteps,N), X :: 2..9, Gifts = new_list(NumGifts), Gifts :: 1..6, % constraints % At the start of this exciting adventure, they each have a different number % and all the numbers are greater than 1 and less than 10. all_different([X[1,I] : I in 1..N]), % step 1 % David gives some (one or more) of his beans to Alice, and Claire gives % some but not all of hers to Boris. % step 2 % Gift 1: David -> Alice X[2,David] #= X[1,David] - Gifts[1], X[2,Alice] #= X[1,Alice] + Gifts[1], Gifts[1] #<= X[1,David], % % Gift 2: Claire to Boris X[2,Claire] #= X[1,Claire] - Gifts[2], X[2,Boris] #= X[1,Boris] + Gifts[2], Gifts[2] #< X[1,Claire], % Then Boris gives some to Alice (lucky Alice!) and David gives some to Claire. % step 3 % Gift 3: Boris -> Alice X[3,Boris] #= X[2,Boris] - Gifts[3], X[3,Alice] #= X[2,Alice] + Gifts[3], Gifts[3] #<= X[2,Boris], % Gift 4: David -> Claire X[3,David] #= X[2,David] - Gifts[4], X[3,Claire] #= X[2,Claire] + Gifts[4], Gifts[4] #<= X[2,David], % All of the numbers of beans given in these transfers are different, % and each is less than 7. all_different(Gifts), % As a result of all that, they again each have a different number, and % again all the numbers are greater than 1 and less than 10. % No child has the same number of beans after as any child % (themselves included) did before. all_different([X[1,I] : I in 1..N] ++ [X[3,I] : I in 1..N]), % At the end, Alice has one more than David and Claire has one % less than Boris. X[3,Alice] #= X[3,David] + 1, X[3,Claire] + 1 #= X[3,Boris], % Alice gets more from David than she does from Boris. Gifts[1] #> Gifts[3], Vars = X.vars ++ Gifts, solve(Vars), println(" A B C D"), foreach(Row in X) println(Row) end, println(gifts=Gifts), nl, fail, nl. go => true.