/* Car in box in Picat. From https://medium.com/puzzle-sphere/can-you-find-the-right-box-d01521f17015 """ There are 3 boxes, exactly one of which has a car. You can keep the car if you pick the correct box! On each box, there is a statement, exactly one of which is true. - Box 1: The car is in this box. - Box 2: The car is not in this box. - Box 3: The car is not in box 1. Which box has the car? """ [t = [0,0,1],car = ]2 Car is in box 2, statement 3 is the true one This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ % import util. import cp. main => go. /* CP */ go ?=> N = 3, Car :: 1..N, T = new_list(N), T :: 0..1, sum(T) #= 1, % Box 1: The car is in this box. T[1] #<=> (Car #= 1), % Box 2: The car is not in this box. T[2] #<=> #~(Car #= 2), % Box 3: The car is not in box 1. T[3] #<=> #~(Car #= 1), Vars = T ++ [Car], solve(Vars), println([t=T,car=Car]), fail, nl. go => true. /* Logic programming [t = [0,0,1],car = 2] */ go2 ?=> N = 3, member(Car,1..N), T = new_list(N), foreach(I in 1..N) member(T[I], 0..1) end, sum(T) == 1, % Box 1: The car is in this box. eq(T[1], Car == 1), % Box 2: The car is not in this box. eq(T[2], Car != 2), % Box 3: The car is not in box 1. eq(T[3],Car != 1), println([t=T,car=Car]), fail, nl. go2 => true. eq(T,C) :- (T == 1 -> C ; not C).