Solve hanging lantern problem: Difference between revisions
→{{header|Raku}}: Add version that numbers lanterns.
m (overflow text) |
(→{{header|Raku}}: Add version that numbers lanterns.) |
||
Line 447:
=={{header|Raku}}==
{{trans|Julia}}
Rather than take the number of columns as an explicit argument, this program infers the number from the size of the array of columns passed in.
==={{header|Sequence as columns}}===
The verbose mode of this version outputs the sequence of columns to remove lanterns from, rather than numbering the lanterns individually as in the description:
<lang perl6>unit sub MAIN(*@columns, :v(:$verbose)=False);
Line 485 ⟶ 488:
[3,3,3,2,1,2]
[3,3,3,2,2,1]</pre>
==={{header|Sequence as lanterns}}===
This longer version numbers the lanterns as in the example in the task description.
<lang perl6>unit sub MAIN(*@columns, :v(:$verbose)=False);
my @sequences = @columns
. pairs
. map({ (.key+1) xx .value })
. flat
. permutations
. map( *.join(',') )
. unique;
if ($verbose) {
my @offsets = |0,|(1..@columns).map: { [+] @columns[0..$_-1] };
my @matrix;
for ^@columns.max -> $i {
for ^@columns -> $j {
my $value = $i < @columns[$j] ?? ($i+@offsets[$j]+1) !! Nil;
@matrix[$j][$i] = $value if $value;;
print "\t" ~ ($value // " ");
}
say '';
}
say "There are {+@sequences} possible takedown sequences:";
for @sequences».split(',') -> @seq {
my @work = @matrix».clone;
my $seq = '[';
for @seq -> $col {
$seq ~= @work[$col-1].pop ~ ',';
}
$seq ~~ s/','$/]/;
say $seq;
}
} else {
say +@sequences;
}</lang>
{{Out}}
<pre>raku lanterns.raku -v 1 2 3 4
1 2 4 7
3 5 8
6 9
10
There are 12600 possible takedown sequences:
[1,3,2,6,5,4,10,9,8,7]
[1,3,2,6,5,10,4,9,8,7]
[1,3,2,6,5,10,9,4,8,7]
[1,3,2,6,5,10,9,8,4,7]
[1,3,2,6,5,10,9,8,7,4]
...
[10,9,8,7,6,5,3,4,1,2]
[10,9,8,7,6,5,3,4,2,1]
[10,9,8,7,6,5,4,1,3,2]
[10,9,8,7,6,5,4,3,1,2]
[10,9,8,7,6,5,4,3,2,1]</pre>
=={{header|VBA}}==
|