Solve hanging lantern problem: Difference between revisions
→{{header|Raku}}: Add direct calculation version.
(Finish grouping the BASICs together) |
(→{{header|Raku}}: Add direct calculation version.) |
||
Line 963:
=={{header|Raku}}==
Note: All of these solutions accept the list of column sizes as command-line arguments and infer the number of columns from the number of sizes provided, rather than requiring that a count be supplied as an extra distinct parameter.
{{trans|Julia}}▼
===
{{trans|Pascal}}
If all we need is the count, then we can compute that directly:
<lang perl6>unit sub MAIN(*@columns);
sub postfix:<!>($n) { [*] 1..$n }
say [+](@columns)! / [*](@columns»!);</lang>
{{Out}}
<pre>$ raku lanterns.raku 1 2 3▼
60</pre>
===Sequence as column numbers===
▲{{trans|Julia}}
If we want to list all of the sequences, we have to do some more work. This version outputs the sequences as lists of column numbers (assigned from 1 to N left to right); at each step the bottommost lantern from the numbered column is removed.
<lang perl6>unit sub MAIN(*@columns, :v(:$verbose)=False);
Line 988 ⟶ 1,003:
{{Out}}
<pre>
▲<pre>$ raku lanterns.raku 1 2 3
$ raku lanterns.raku --verbose 1 2 3
There are 60 possible takedown sequences:
Line 1,006 ⟶ 1,020:
[3,3,3,2,2,1]</pre>
===Sequence as
<lang perl6>unit sub MAIN(*@columns, :v(:$verbose)=False);
Line 1,045 ⟶ 1,059:
{{Out}}
<pre>$ raku lanterns.raku -v 1 2 3
1 2 4
3 5
6
▲There are 12600 possible takedown sequences:
[1,3,6,2
[1,3
▲[1,3,2,6,5,10,9,4,8,7]
...
[
[
[
=={{header|Wren}}==
|