/* Baseball cap color problem in Picat. From https://stackoverflow.com/questions/61873172/finding-cap-color-in-an-answer-set-programming-program """ Three baseball players, one playing for the Blue Birds, one playing for the Brown Bears and one playing for the Red Foxes, meet in the dressing room one day. One player was wearing a blue cap, another was wearing a brown cap and the third was wearing a red cap. "Isn't it funny," said the player from the Red Foxes to the others, "that not one of us is wearing a cap which matches the colour on our teams?" "That's true," agreed the player wearing the brown cap. Can you now say what colour cap each man was wearing? """ 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, Team = [Red,Brown,_Blue], Team = 1..N, Colors = [red,brown,blue], Caps = new_list(3), Caps :: 1..N, all_different(Caps), % Cannot be the same color as team (i.e. no fixpoint) foreach(I in 1..3) Caps[I] #!= I end, % The extra hint: The player from the Red team cannot wear a brown cap Caps[Red] #!= Brown, solve(Caps), println(caps=Caps), println([ [team=Colors[Team[I]],cap=Colors[Caps[I]]] : I in 1..N]), fail, nl. go => true.