Reputation: 418
Is it possible to have the numbers 1-9 be placed such that it fills a constrain set out.
I have:
foo(bl1,A,B,C,Total1) :- A+B+C =:= Total1.
foo(bl2,A,B,D,Total2) :- A+B+D =:= Total2.
run_foo(A,B,C,D) :-
foo(bl1,A,B,C,13),
foo(bl2,A,B,D,11),
A/=B,
A/=C,
A/=D,
B=/C,
B=/D,
C=/D.
And then run with something like:
run_foo(A,6,C,D).
So that it returns the values of A
, C
and D
. This should return A=4
,C=3
, D=2
.
Upvotes: 0
Views: 52
Reputation: 3059
You can use a constraints solver:
?- B #= 6, Vs = [A,B,C,D],
Vs ins 1..9, A+B+C #= 13, A+B+D #= 11, all_distinct(Vs), label(Vs).
B = 6, Vs = [2,6,5,3], A = 2, C = 5, D = 3
; B = 6, Vs = [3,6,4,2], A = 3, C = 4, D = 2
; B = 6, Vs = [4,6,3,1], A = 4, C = 3, D = 1.
Or:
?- B = 6, Vs = [A,B,C,D],
maplist(between(1,9), Vs), A+B+C =:= 13, A+B+D =:= 11, \+ (select(V, Vs, Vs0), member(V, Vs0)).
B = 6, Vs = [2,6,5,3], A = 2, C = 5, D = 3
; B = 6, Vs = [3,6,4,2], A = 3, C = 4, D = 2
; B = 6, Vs = [4,6,3,1], A = 4, C = 3, D = 1
; false.
Upvotes: 1