/* Guards and apple problem in Picat. https://gist.github.com/2484152 """ a boy wants to give an apple to a girl. to get to her, he has to pass through five gates, each with a guard. he bribes each guard with half of his apples, plus one. after he's given the apple to the girl, he has no apples left. how many did he have to begin with? """ 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 ?=> N = 5, X = new_list(N+1), X :: 0..1000, X[N+1] #= 1, % apples left % N guards foreach(I in 1..N) X[I+1] #= (X[I] div 2) - 1, X[I] mod 2 #= 0 end, solve(X), println(numApples=X[1]), println(x=X), fail, nl. go => true.