Dinesman's multiple-dwelling problem: Difference between revisions

Content deleted Content added
Util (talk | contribs)
m →‎{{header|Perl 6}}: Change adjacency expression
→‎{{header|Perl 6}}: various simplifications
Line 296:
 
=={{header|Perl 6}}==
We use permutations because "different floors" are specified. The next_perm subroutine is a variant of the one from the [[Permutations]] task.
<lang perl6>sub next_perm ( @a is copy ) {
my $j = @a.end - 1;
$jreturn Nil if -- while $j >=< 10 andwhile [gt>] @a[ $j, $j+1 ];
 
my $aj = @a[$j];
my $k = @a.end;
$k-- while [gt>] $aj, @a[$k];
@a[ $j, $k ] .= reverse;
 
my Int $r = @a.end;
my Int $s = $j + 1;
@a[ $r--, $s++ ] .= reverse while $r > $s {;
@a[ $r, $s ] .= reverse;
$r--;
$s++;
}
return @a;
}
 
# Contains only five floors. 5! = 120 permutations.
for ( [1..5], {.&next_perm} ...^ !* )[^120] -> [ $b, $c, $f, $m, $s ] {
say "Baker=$b Cooper=$c Fletcher=$f Miller=$m Smith=$s"
if $b != 5 # Baker !live on top floor.