Department numbers: Difference between revisions

m (→‎Using a constraint solver: use tuples instead of lists)
Line 2,277:
4 5 3</syntaxhighlight>
 
===Alternate approachapproaches===
 
<syntaxhighlight lang="j"> (/:"#. 2|]) (#~ 12=+/"1) 1+3 comb 7 [ load'stats'
Line 2,287:
 
Note that we are only showing the distinct valid [[combinations]] here, not all valid [[permutations]] of those combinations. (Valid permutations would be: swapping the last two values in a combination, and all permutations of 2 4 6.)
 
Another variation would be more constraint based, blindly implementing the rules of the task and yielding all valid permutations:
 
<syntaxhighlight lang=J> NB. 3 departments, 1..7 in each
rule1=. >,{3#<1+i.7
NB. total must be 12, numbers must be unique
rule2=. (#~ ((3=#@~.) * 12=+/)"1) rule1
NB. no odd numbers in police department (first department)
rule3=. (#~ 0=2|{."1) rule2
rule3
2 3 7
2 4 6
2 6 4
2 7 3
4 1 7
4 2 6
4 3 5
4 5 3
4 6 2
4 7 1
6 1 5
6 2 4
6 4 2
6 5 1</syntaxhighlight>
 
=={{header|Java}}==
6,951

edits