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.
I feel that my solution is more clunky than needs to be:
:- use_module(library(clpfd)).
check(AJ, AK, AL, AM, CJ, CK, CL, CM, SJ, SK, SL, SM) :-
AJ in 15..16 \/ 18..19,
AK in 15..16 \/ 18..19,
AL in 15..16 \/ 18..19,
AK in 15..16 \/ 18..19,
all_different([AJ, AK, AL, AM]),
CJ in 1..4, CK in 1..4, CL in 1..4, CM in 1..4,
SJ in 1..4, SK in 1..4, SL in 1..4, SM in 1..4,
all_different([CJ, CK, CL, CM]),
all_different([SJ, SK, SL, SM]),
CL #< CM,
CJ #= 3,
( AJ #= 16, CJ #= 1 ;
AK #= 16, CK #= 1 ;
AL #= 16, CL #= 1 ;
AM #= 16, CM #= 1 ),
SL #= 2,
( AJ #= 15, SJ #= 1 ;
AK #= 15, SK #= 1 ;
AL #= 15, SL #= 1 ;
AM #= 15, SM #= 1 ),
AK #= AM - 3,
( CJ #= 4, SJ #= 3 ;
CK #= 4, SK #= 3 ;
CL #= 5, SL #= 3 ;
CM #= 4, SM #= 3 ),
( CJ #= SJ, CK #\= SK, CL #\= SL, CM #\= SM ;
CJ #\= SJ, CK #= SK, CL #\= SL, CM #\= SM ;
CJ #\= SJ, CK #\= SK, CL #= SL, CM #\= SM ;
CJ #\= SJ, CK #\= SK, CL #\= SL, CM #= SM ).
Is there a better way to express the constraints?
Improved version after suggestions:
:- use_module(library(clpfd)).
check(AJ, AK, AL, AM, CJ, CK, CL, CM, SJ, SK, SL, SM) :-
permutation([AJ, AK, AL, AM], [15, 16, 18, 19]),
permutation([CJ, CK, CL, CM], [1, 2, 3, 4]),
permutation([SJ, SK, SL, SM], [1, 2, 3, 4]),
CL #< CM,
CJ #= 3,
( AJ #= 16, CJ #= 1 ;
AK #= 16, CK #= 1 ;
AL #= 16, CL #= 1 ;
AM #= 16, CM #= 1 ),
SL #= 2,
( AJ #= 15, SJ #= 1 ;
AK #= 15, SK #= 1 ;
AL #= 15, SL #= 1 ;
AM #= 15, SM #= 1 ),
AK #= AM - 3,
( CJ #= 4, SJ #= 3 ;
CK #= 4, SK #= 3 ;
CL #= 5, SL #= 3 ;
CM #= 4, SM #= 3 ),
( CJ #= SJ, CK #\= SK, CL #\= SL, CM #\= SM ;
CJ #\= SJ, CK #= SK, CL #\= SL, CM #\= SM ;
CJ #\= SJ, CK #\= SK, CL #= SL, CM #\= SM ;
CJ #\= SJ, CK #\= SK, CL #\= SL, CM #= SM ).