Perl: ∞
$z.="-1,";$_.=A;END{$m.=!!s/./{${z}B}/g,map(/B/||s/\d+\K/,/g*/(-\d*,).*\1/||($n{$_}||=$m++),sort glob),print$n{$z}if/A/}
$z.="-2,";$_.=A;END{$m.=!!s/./{${z}B}/g,map(/B/||s/\d+\K/,/g*/(-\d*,).*\1/||($n{$_}||=$m++),sort glob),print$n{$z}if/A/}
$z.="-3,";$_.=A;END{$m.=!!s/./{${z}B}/g,map(/B/||s/\d+\K/,/g*/(-\d*,).*\1/||($n{$_}||=$m++),sort glob),print$n{$z}if/A/}
Extend to any length you like
Will quickly run out of memory since memory usage is like O(n^n). It would however be easy to replace the permutation indexer by O(n) code, just longer. I'm just illustrating the way you can use END{}
for this task in perl. All END{}
blocks run at exit time, but only the first one that get called (the one last in the code) will output anything due to the /A/
test which is only true once
Notice that the $m
counter must count as a string because as a number it would overflow (later than the end of the universe but it's the principle that counts). For the same reason I "count" the number of lines by constructing a string of A
s instead of using a real counter though this overflow would happen even later.
Another way to do this in perl:
@{$b.=A; if($a eq $b) {use bigint; $n=0;$n++for@F;$c=0;$c=$c*$n--+map{$z=$_;$a=grep$_&&$_>$z,@F;$_=0;}@F;print$c}}=()x push@F,"1".!($a.=A),
@{$b.=A; if($a eq $b) {use bigint; $n=0;$n++for@F;$c=0;$c=$c*$n--+map{$z=$_;$a=grep$_&&$_>$z,@F;$_=0;}@F;print$c}}=()x push@F,"2".!($a.=A),
@{$b.=A; if($a eq $b) {use bigint; $n=0;$n++for@F;$c=0;$c=$c*$n--+map{$z=$_;$a=grep$_&&$_>$z,@F;$_=0;}@F;print$c}}=()x push@F,"3".!($a.=A),
This uses that fact that in foo = bar
bar
is executed after foo
. This version by the way doesn't go crazy in time and space but that makes the code longer
Yet another idea is to use DESTROY
which has the advantage that only one of them will be executed. I'm not going to repeat the permutation indexing code of which I already gave two examples.
push@F,"1";bless\@F;DESTROY{print"Work out permutation index of @{$_[0]}\n" }
push@F,"2";bless\@F;DESTROY{print"Work out permutation index of @{$_[0]}\n" }
push@F,"3";bless\@F;DESTROY{print"Work out permutation index of @{$_[0]}\n" }
Or using BEGIN
:
BEGIN{push@F,"1"} print"Work out permutation index of @F\n"; exit;
BEGIN{push@F,"2"} print"Work out permutation index of @F\n"; exit;
BEGIN{push@F,"3"} print"Work out permutation index of @F\n"; exit;
n
? Are they all tied at a score of ∞? \$\endgroup\$