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.
Directly computing the count
If all we need is the count, then we can compute that directly:
unit subMAIN(*@columns);subpostfix:<!>($n) { [*] 1..$n }say [+](@columns)! / [*](@columns»!);
Output:
$ raku lanterns.raku 1 2 3
60
Sequence as column numbers
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.
unit subMAIN(*@columns, :v(:$verbose)=False);my @sequences = @columns . pairs . map({ (.key+1) xx .value }) . flat . permutations . map( *.join(',') ) . unique;if ($verbose) {say"There are {+@sequences} possible takedown sequences:";say"[$_]"for @sequences;} else {say +@sequences;}
Output:
$ raku lanterns.raku --verbose 1 2 3
There are 60 possible takedown sequences:
[1,2,2,3,3,3]
[1,2,3,2,3,3]
[1,2,3,3,2,3]
[1,2,3,3,3,2]
[1,3,2,2,3,3]
[1,3,2,3,2,3]
...
[3,3,2,2,3,1]
[3,3,2,3,1,2]
[3,3,2,3,2,1]
[3,3,3,1,2,2]
[3,3,3,2,1,2]
[3,3,3,2,2,1]
Sequence as lantern numbers
If we want individually-numbered lanterns in the sequence instead of column numbers, as in the example given in the task description, that requires yet more work: