/* Houses problem in Picat. From Kanren example http://kanren.cvs.sourceforge.net/viewvc/kanren/kanren/examples/houses.scm?view=markup """ Taken from _Algebra 1_, Glencoe/McGraw-Hill, New York, New York, 1998 pg. 411, Problem 56 There are 8 houses on McArthur St, all in a row. These houses are numbered from 1 to 8. Allison, whose house number is greater than 2, lives next door to her best friend, Adrienne. Belinda, whose house number is greater than 5, lives 2 doors away from her boyfriend, Benito. Cheri, whose house number is greater than Benito's, lives three doors away from her piano teacher, Mr. Crawford. Daryl, whose house number is less than 4, lives 4 doors from his teammate, Don. Who lives in each house? """ 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 = 8, All = [Allison, Belinda, Adrienne, Benito, Cheri, Crawford, Daryl, Don], All :: 1..N, AllStr = ["allison", "belinda", "adrienne", "benito", "cheri", "crawford", "daryl", "don"], % who lives in which house Houses = new_list(N), Houses :: 1..N, all_different(All), all_different(Houses), % Allison, whose house number is greater than 2, lives next door % to her best friend, Adrienne. Allison #> 2, abs(Allison-Adrienne) #= 1, % Belinda, whose house number is greater than 5, % lives 2 doors away from her boyfriend, Benito. Belinda #> 5, abs(Belinda-Benito) #= 2, % Cheri, whose house number is greater than Benito's, lives % three doors away from her piano teacher, Mr. Crawford. Cheri #> Benito, abs(Cheri-Crawford) #= 3, % Daryl, whose house number is less than 4, % lives 4 doors from his teammate, Don. Daryl #< 4, abs(Daryl-Don) #= 4, % channeling the persons and houses, i.e. % place each person in proper house assignment(All, Houses), Vars = All ++ Houses, solve(Vars), println(all=All), println(houses=Houses), foreach(I in 1..N) printf("House %d: %w\n", I ,AllStr[Houses[I]]) end, nl, fail, nl. go => true.