/* Knight and knaves: A spy appars in Picat. From Adrian Groza "Modelling Puzzles in First Order Logic" """ Puzzle 69. A spy appears On the island of knights and knaves a new type on inhabitants has settled: spies. Spies can lie or tell the truth at will. You are approached by three people wearing different coloured clothes. You know that one is a knight, one is a knave, and one is a spy. They speak in the following order: The man wearing blue says, "I am a knight". The man wearing red says, "He speaks the truth". The man wearing green says, "I am a spy". Who is the knight, who is the knave, and who is the spy? (taken from Popular mechan- ics - www.popularmechanics.com/science/math) """ ts = [1,1,0] type = [1,2,0] Blue: Knight Red: Spy Green: Knave This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go ?=> Knave = 0, Knight = 1, Spy = 2, Type = [Blue,Red,Green], % Type Type :: [Knave,Knight,Spy], % Speak truth Ts = [BlueT,RedT,GreenT], % Truth Ts :: 0..1, % You know that one is a knight, one is a knave, and one is a spy. all_different(Type), % The man wearing blue says, "I am a knight". BlueT #<=> Blue #= Knight, % The man wearing red says, "He speaks the truth". RedT #<=> BlueT #= 1, % The man wearing green says, "I am a spy". GreenT #<=> Green #= Spy, % Who is the knight, who is the knave, and who is the spy? foreach(I in 1..3) Ts[I] #= 1 #=> Type[I] :: [Knight,Spy], Ts[I] #= 0 #=> Type[I] :: [Knave,Spy] end, Vars = Ts ++ Type, solve(Vars), println(ts=Ts), println(type=Type), Map = new_map([0="Knave",1="Knight",2="Spy"]), printf("Blue: %w Red: %w Green: %w\n",Map.get(Blue), Map.get(Red),Map.get(Green)), fail, nl. go => true.