/* Logic Puzzle from The Art of Prolog in Picat. Sterling, Shapiro: The Art of Prolog (2nd edition), page 258 """ Three friends come first, second, and third in a programming competition. Each of the three has a different first name, likes a different sport, and has a different nationality. Michael likes baskeball and did better than the American, Simon, the Israeli, did better than the tennis player. The cricket player came first. Who is the Australian? What sport does Richard play? """ Answer: Michael is the Australian and Richard plays tennis. 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 = 3, People = [Michael, Simon, _Richard], People = 1..N, PeopleS = [michael, simon, richard], Sport = [Basketball, Tennis, Cricket], Sport :: 1..N, SportS = [basketball, tennis, cricket], Country = [America, Israel, _Australia], Country :: 1..N, CountryS = [america, israel, australia], Place = new_list(N), Place :: 1..N, all_different(Country), all_different(Place), all_different(Sport), % Michael likes baskeball and did better than the American, Michael #= Basketball, element(America,Place,PlaceAmerica), Place[Michael] #< PlaceAmerica, % Simon, the Israeli, did better than the tennis player. Simon #= Israel, element(Tennis,Place,PlaceTennis), Place[Simon] #< PlaceTennis, % The cricket player came first. % Place[Cricket] #= 1, element(Cricket,Place,1), Vars = Country ++ Place ++ Sport, solve(Vars), println(sport=Sport), println(country=Country), println(place=Place), nl, foreach(I in 1..N) element(S,Sport,I), element(C,Country,I), println([PeopleS[I],SportS[S],CountryS[C],placed=Place[I]]) end, nl, fail, nl. go => true.