/* Knight and knaves: Who is the spy? Picat. From Adrian Groza "Modelling Puzzles in First Order Logic" """ Puzzle 70. Who is the spy? On the island of knights and knaves and spies, you come across three people. One wears blue, one wears red, and one wears green. You know that one is a knight, one is a knave, and one is a spy. "Who is the spy?" you ask. The man wearing blue says, "That man in red is the spy". The man wearing red says, "No, the man in green is the spy". The man wearing green says, "No, the man in red is in fact the spy". (taken from Popular mechanics - www.popularmechanics.com/science/math) """ ts = [0,1,0] type = [0,1,2] Blue: Knave Red: Knight Green: Spy 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), % "Who is the spy?" you ask. % The man wearing blue says, "That man in red is the spy". BlueT #<=> Red #= Spy, % The man wearing red says, "No, the man in green is the spy". RedT #<=> Green #= Spy, % The man wearing green says, "No, the man in red is in fact the spy". GreenT #<=> Red #= 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.