/* Four trees in Picat. http://l4f.cecs.anu.edu.au/puzzles/beginner/four-trees """ Four trees are planted at the corners of a square. They are an oak, an elm, an ash and a fir. I walk around three sides of the square, thus passing all four trees once. I notice that the oak and ash are not adjacent. I passed the fir before either the elm or the oak, and I neither started nor finished at the ash. In what order did I pass the trees? """ 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 = 4, Position = [Oak,Elm,Ash,Fir], Position :: 1..N, S = [oak,elm,ash,fir], Order = new_list(N), Order :: 1..N, all_different(Position), % I notice that the oak and ash are not adjacent. abs((Oak mod (N+1)) - (Ash mod (N+1))) #> 1, % I passed the fir before either the elm or the oak, Fir #< Elm, Fir #< Oak, % and I neither started nor finished at the ash. Ash #> 1, Ash #< N, assignment(Position,Order), Vars = Position ++ Order, solve(Vars), println(position=Position), println('order '=Order), println('order '=S.lookup(Order)), nl, fail, nl. go => true. % % Lookup = lookup(L,Ix) % Lookup = L.lookup(Ix) % % returns a list where the indices in A[I] is looked up in L. % lookup(L,Ix) = [ L[Ix[I]] : I in 1..L.len ].