/* Scrabble Contest puzzle in Picat. From https://www.braingle.com/brainteasers/24501/scrabble-contest.html """ At the local games evening, four lads were competing in the Scrabble and chess competitions. Liam beat Mark in chess, James came third and the 16 year old won. Liam came second in Scrabble, the 15 year old won; James beat the 18 year old and the 19 year old came third. Kevin is 3 years younger than Mark. The person who came last in chess, came third in Scrabble and only one lad got the same position in both games. Can you determine the ages of the lads and the positions in the two games? """ Answer shoulAnswer """ James (15) Scrabble: 1st - Chess: 3rd Kevin(16) Scrabble: 4th - Chess: 1st Liam (18) Scrabble: 2nd - Chess: 2nd Mark (19) Scrabble: 3rd - Chess: 4th """ This model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. import v3_utils. main => go. go ?=> % At the local games evening, four lads were competing in the Scrabble and % chess competitions. N = 4, Lads = [James,Kevin,Liam,Mark], Lads = 1..Lads.len, LadsS = ['James','Kevin','Liam','Mark'], % Position Chess Chess = new_list(N), Chess :: 1..N, % Position Scrabble Scrabble = new_list(N), Scrabble :: 1..N, Ages = new_list(N), Ages :: [15,16,18,19], all_different(Chess), all_different(Scrabble), all_different(Ages), element(Ages15,Ages,15), element(Ages16,Ages,16), element(Ages18,Ages,18), element(Ages19,Ages,19), % Liam beat Mark in chess, James came third and the 16 year old won. Chess[Liam] #< Chess[Mark], Chess[James] #= 3, element(Ages16,Chess,1), % Liam came second in Scrabble, the 15 year old won; James beat the 18 year old % and the 19 year old came third. Scrabble[Liam] #= 2, element(Ages15,Scrabble,1), element(Ages18,Scrabble,ScrabbleAges18), Scrabble[James] #< ScrabbleAges18, element(Ages19,Scrabble,3), % Kevin is 3 years younger than Mark. Ages[Kevin] + 3 #= Ages[Mark], % The person who came last in chess, came third in Scrabble and only one lad % got the same position in both games. element(ChessPlace4,Chess,4), element(ChessPlace4,Scrabble,3), sum([Chess[I] #= Scrabble[I] : I in 1..N]) #= 1, % Can you determine the ages of the lads and the positions in the two games? Vars = Chess ++ Scrabble ++ Ages ++ [Ages15,Ages16,Ages18,Ages19], solve($[],Vars), println('chess '=Chess), println(scrabble=Scrabble), println('ages '=Ages), nl, foreach(I in 1..N) printf("%-5w (%2d) Scrabble=%d Chess=%d\n", LadsS[I],Ages[I],Scrabble[I],Chess[I]) end, nl, fail, nl. % % Prolog style, i.e. % - no array access (just element) % - no list comprehension % - length/2 instead of new_list/1 % go2 :- % At the local games evening, four lads were competing in the Scrabble and % chess competitions. N = 4, length(Lads,N), Lads = [James,Kevin,Liam,Mark], Lads = [1,2,3,4], % LadsS = ['James','Kevin','Liam','Mark'], % Lads ins 1..N, % SWI-Prolog Lads :: 1..N, length(Chess,N), % Chess ins 1..N, Chess :: 1..N, length(Scrabble,N), % Scrabble ins 1..N, Scrabble :: 1..N, length(Ages,N), % Ages ins 15 \/ 16 \/ 18 \/ 19, Ages :: [15,16,18,19], all_different(Chess), all_different(Scrabble), all_different(Ages), element(Ages15,Ages,15), element(Ages16,Ages,16), element(Ages18,Ages,18), element(Ages19,Ages,19), % Liam beat Mark in chess, James came third and the 16 year old won. element(Liam,Chess,ChessLiam), element(Mark,Chess,ChessMark), ChessLiam #< ChessMark, element(James,Chess,3), element(Ages16,Chess,1), % Liam came second in Scrabble, the 15 year old won; James beat the 18 year old % and the 19 year old came third. element(Liam,Scrabble,2), element(Ages15,Scrabble,1), element(Ages18,Scrabble,ScrabbleAges18), element(James,Scrabble,ScrabbleJames), ScrabbleJames #< ScrabbleAges18, element(Ages19,Scrabble,3), % Kevin is 3 years younger than Mark. element(Kevin,Ages,AgesKevin), element(Mark,Ages,AgesMark), AgesKevin + 3 #= AgesMark, % The person who came last in chess, came third in Scrabble and only one lad % got the same position in both games. element(ChessPlace4,Chess,4), element(ChessPlace4,Scrabble,3), sums(Chess,Scrabble,0,Sums), Sums #= 1, % Can you determine the ages of the lads and the positions in the two games? flatten([Chess,Scrabble,Ages,Sums],Vars), solve(Vars), writeln('chess '=Chess), writeln('scrabble'=Scrabble), writeln('ages '=Ages), nl, fail, nl. go2. sums([],[],Total,Total). sums([C|Chess],[S|Scrabble],Total0,Total) :- B :: 0..1, C #= S #<=> B #= 1, Total1 #= Total0 + B, sums(Chess,Scrabble,Total1,Total).