/* Golfers and pants problem in Picat. Via Pascal Bercker "Those Hideous Green Golf Pants — A Constraint Satisfaction Problem — With Netica" https://medium.com/@pbercker/those-hideous-green-golf-pants-a-constraint-satisfaction-problem-with-netica-16435fa58f34 """ A foursome of golfers (Fred, Joe, Bob, Tom) is standing at a tee, in a line from left to right. Each golfer wears diffrent colored pants: * one is wearing red pants; * the golfer to Fred’s immediate right is wearing blue pants; * Joe is second in line; * Bob is wearing plaid pants; * Tom isn’t in position one or four, and he isn’t wearing the hideous orange pants. In what order are the four golfers standing at the tee, and what color are each golfer’s pants? Source Antoni Niederliński "A Gentle Guid to Constraint Logic Programming via ECLiPSe" 3rd edition (page 50) """ Solution: Fred is in position 1 and wears orange pants Joe is in position 2 and wears blue pants Tom is in position 3 and wears red pants Bob is in position 4 and wears plaid pants Original source of this problem is (according to Niederliński): Friedman-Hill, E., Jess in Action. Rule-Based Systems in Java, 2003. This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. % CP go ?=> People = [Fred,Joe,Bob,Tom], N = People.len, People = 1..N, % To which person does these pants belong to Pants = [_Red,Blue,Plaid,Orange], Pants :: 1..N, % Which person has position Position[I] Position = new_list(N), Position :: 1..N, all_different(Pants), all_different(Position), % * one is wearing red pants % * the golfer to Fred’s immediate right is wearing blue pants element(Fred,Position,FredPos), % Position[FredPos] #= Fred element(Blue,Position,BluePos), % Position[BluePos] #= Blue FredPos + 1 #= BluePos, % * Joe is second in line Position[2] #= Joe, % * Bob is wearing plaid pants Bob #= Plaid, % * Tom isn’t in position one or four, and he isn’t wearing the hideous orange % pants Position[1] #!= Tom, Position[4] #!= Tom, Tom #!= Orange, Vars = Pants ++ Position, solve(Vars), % Neat presentation PeopleS = ["Fred","Joe","Bob","Tom"], PantsS = ["red","blue","plaid","orange"], foreach(Pos in 1..N) element(Person,Position,Pos), element(Person,Pants,Pa), printf("%w is in position %d and wears %w pants\n",PeopleS[Person],Pos,PantsS[Pa]) end, fail, nl. go => true.