Reputation: 1
For example, I have a safe that needs an answer of length 5 (XXXXX) numbers but you only use 1,2,3. So some combos could be 1,2,3 & 1,1,1 & 1,1,2 etc... but I need to find all possibilities.
I have brainstormed and I think I need to find all permutations of all combos of the three numbers but I can't figure out how to find the answer in size 5.
If it was just three numbers and the answer was length 3 I could just use the permutation function but this doesn't work since I need it to be a larger size than elements used.
Upvotes: 0
Views: 57
Reputation:
No problemo, use prolog-lists:
rebmem(L,Y) :- member(Y,L).
?- length(Zs,5), maplist(rebmem([1,2,3]),Zs).
Zs = [1, 1, 1, 1, 1] ;
Zs = [1, 1, 1, 1, 2] ;
Zs = [1, 1, 1, 1, 3] ;
Zs = [1, 1, 1, 2, 1] ;
Zs = [1, 1, 1, 2, 2] ;
Zs = [1, 1, 1, 2, 3] ;
Zs = [1, 1, 1, 3, 1] ;
Zs = [1, 1, 1, 3, 2] ;
Zs = [1, 1, 1, 3, 3] ;
Zs = [1, 1, 2, 1, 1]
Upvotes: 0
Reputation: 18726
No problemo with clpfd!
?- use_module(library(clpfd)). true. ?- length(Zs, 5), Zs ins 1..3, labeling([], Zs). Zs = [1, 1, 1, 1, 1] ; Zs = [1, 1, 1, 1, 2] ; Zs = [1, 1, 1, 1, 3] ; Zs = [1, 1, 1, 2, 1] ; Zs = [1, 1, 1, 2, 2] ; Zs = [1, 1, 1, 2, 3] ; Zs = [1, 1, 1, 3, 1] ; Zs = [1, 1, 1, 3, 2] ; Zs = [1, 1, 1, 3, 3] ; Zs = [1, 1, 2, 1, 1] ...
Upvotes: 2