/* Friends Reunion Puzzle in Picat. From Rajul Saxena "The Friends Reunion Interview Puzzle" https://medium.com/puzzle-sphere/friends-reunion-interview-seating-arrangement-puzzle-math-logic-apple-google-amazon-microsoft-faang-quant-5cb57af547c7 """ Four school friends — Aarti(F), Bhavesh(M), Chitra(F), and Dev(M) — gathered for a reunion after many years. - All of them pursued different careers. One is a teacher, another a lawyer, the third a doctor, and the fourth an engineer. - They sit around a square table, and based on their seating arrangement, some clues about their professions are given below: 1. The teacher sat to the left of Aarti. 2. The doctor sat directly opposite Bhavesh. 3. Chitra and Dev sat next to each other. 4. A woman sat to the left of the lawyer. Given these hints, can you determine who among them is the engineer? """ [Aarti,Engineer,1] [Bhavesh,Teacher,4] [Chitra,Doctor,2] [Dev,Lawyer,3] Positions: Aarti Bhavesh Chitra Dev This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ % import util. import cp. main => go. go ?=> N =4, Names = [Aarti,Bhavesh,Chitra,_Dev], Names = 1..N, NamesS = ["Aarti","Bhavesh","Chitra","Dev"], % F = 1, % Female % M = 2, % Gender % Gender = [F,M,F,M], Job = [Teacher,Lawyer,Doctor,_Engineer], Job :: 1..N, JobS = ["Teacher","Lawyer","Doctor","Engineer"], all_different(Job), % % Positions % 1 % ----- % 4 | | 2 % ----- % 3 % Position = [APos,BPos,CPos,DPos], Position :: 1..4, all_different(Position), % They sit around a square table, and based on their seating arrangement, % some clues about their professions are given below: element(Teacher,Position,TeacherPos), element(Doctor,Position,DoctorPos), element(Lawyer,Position,LawyerPos), % element(Engineer,Position,EngineerPos), % 1. The teacher sat to the left of Aarti. (APos mod N) - (TeacherPos mod N) #= 1, Teacher #!= Aarti, % 2. The doctor sat directly opposite Bhavesh. abs( (DoctorPos mod N) - (BPos mod N)) #= 2, Doctor #!= Bhavesh, % 3. Chitra and Dev sat next to each other. abs( (CPos mod N) - (DPos mod N)) #= 1, % 4. A woman sat to the left of the lawyer. LeftLawyer #= Aarti #\/ LeftLawyer #= Chitra, LeftLawyerPos :: 1..N, element(LeftLawyer,Position,LeftLawyerPos), (LawyerPos mod N) - (LeftLawyerPos mod N) #= 1, LeftLawyer #!= Lawyer, APos #= 1, % Symmetry breaking: Aarti in position 1 % For easier handling of the positions APosition = new_list(N), APosition :: 1..N, assignment(Position,APosition), Vars = Job ++ Position ++ [LeftLawyer], solve(Vars), foreach(I in 1..N) element(J,Job,I), element(P,APosition,I), println([NamesS[I],JobS[J],P]) end, nl, println("Positions:"), printf(" %-10w \n", NamesS[APosition[1]]), printf("%-10w %-10w\n", NamesS[APosition[4]], NamesS[APosition[2]]), printf(" %-10w \n", NamesS[APosition[3]]), nl, fail, nl. go => true.