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}}
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.
 
===SequenceDirectly ascomputing columnsthe count===
{{trans|Pascal}}
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:
 
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
60
$ 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 lanternslantern numbers===
ThisIf longerwe versionwant numbersindividually-numbered lanterns in the lanternssequence instead of column numbers, as in the example given in the task description., that requires yet more work:
 
<lang perl6>unit sub MAIN(*@columns, :v(:$verbose)=False);
Line 1,045 ⟶ 1,059:
 
{{Out}}
<pre>$ raku lanterns.raku -v 1 2 3 4
1 2 4 7
3 5 8
6 9
There are 1260060 possible takedown sequences:
10
[1,3,2,6,5,10,9,4,8,7]
There are 12600 possible takedown sequences:
[1,3,6,2,6,5,4,10,9,8,7]
[1,3,2,6,5,102,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,3,2]
[10,9,8,7,6,5,4,3,41,2,1]
[10,9,8,7,6,5,4,1,3,2,1]</pre>
[10,9,8,7,6,5,4,3,1,2]
[10,9,8,7,6,5,4,3,2,1]</pre>
 
=={{header|Wren}}==
1,479

edits