/* Tennis problem in Picat. From Jean-Louis Laurière "Problem Solving and Artificial Intelligence", page 422pp: """ Frank and George play tennis. Frank beats George 6 game to 3 In 4 games the server loses. Who served the first game? """ There are 40 solutions to this problem. For all solutions George serves first, i.e. gs[1] = 1. 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 ?=> puzzle(Fs,Gs,Fw,Gw,Gs1), println(fs=Fs), println(fw=Fw), println(gs=Gs), println(gw=Gw), println(george_serves_first=Gs1), nl, fail, nl. go => true. go2 => All = find_all(Gs1,puzzle(_Fs,_Gs,_Fw,_Gw,Gs1)), println(all=All), println(len=All.len), nl. puzzle(Fs,Gs,Fw,Gw,Gs1) => N = 9, % 6 + 3 games % decision variables Fs = new_list(N), % 1 if Frank serves Fs :: 0..1, Gs = new_list(N), % 1 if George serves Gs :: 0..1, Fw = new_list(N), % 1 if Frank wins game Fw :: 0..1, Gw = new_list(N), % 1 if Geoge wins game Gw :: 0..1, foreach(J in 1..N) Fs[J] #= 1-Gs[J], Fw[J] #= 1-Gw[J] end, foreach(J in 2..N) Gs[J] #= 1-Gs[J-1] end, % frank wins 6 games sum(Fw) #= 6, % sum(Gw) #= 3, % redundant % in 4 games the serves loses sum([Fs[J]*Gw[J] + Gs[J]*Fw[J] : J in 1..N]) #= 4, Gs1 #= Gs[1], Vars = Fs ++ Gs ++ Fw ++ Gw, solve([],Vars).