/* Initials queue puzzle in Picat. From Chris Smith' Math Newsletter #568 ''' I noticed something weird about names of the first 10 people in the queue for a Maths lecture: [Image of 10 people in a queue.] Each of them had initials which were an alphabetically ordered pair of distinct letters from the first five letters of the alphabet (A,B,C,D,E)! None of the people had the same initials as any other and no-one shared a letter with the person in front of them. Mathematician BE was at the front of the queue with CD right behind, while BD was right at the end of the queue. Can you use this information to work out the initials of the ten people in the queue? ''' 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 ?=> N = 10, A = 1, B = 2, C = 3, D = 4, E = 5, Queue = new_array(N,2), Queue :: A..E, % constraints % alphabetically ordered pairs of distinct letters foreach(I in 1..N) Queue[I,1] #< Queue[I,2] end, % None of the people had the same initials as any other all_different_pairs(N,Queue), % no-one shared a letter with the person in front of them foreach(I in 1..N-1) all_different([Queue[I,1],Queue[I,2],Queue[I+1,1],Queue[I+1,2]]) end, Queue[1,1] #= B, % BE in front Queue[1,2] #= E, Queue[2,1] #= C, % CD right behind Queue[2,2] #= D, Queue[N,1] #= B, % BD at the end Queue[N,2] #= D, solve(Queue), Cs = "ABCDE", println(Queue), print("Queue: "), foreach(I in 1..N) printf("%w ",[Cs[Queue[I,J]] : J in 1..2]) end, nl, fail, nl. go => true. % % ensure all pairs are different % all_different_pairs(N,X) => all_different($[X[K,1]*(N-1) + X[K,2] : K in 1..X.len]).