aboutsummaryrefslogtreecommitdiff
path: root/challenge-191
diff options
context:
space:
mode:
authorLuis Mochan <mochan@fis.unam.mx>2022-11-17 09:55:18 -0600
committerLuis Mochan <mochan@fis.unam.mx>2022-11-17 09:55:18 -0600
commit8e79725397c7bd8166530842cd3fdc3eb031cdca (patch)
tree869788dd606af1eec26b9d8d4b85883df1f287d6 /challenge-191
parentf09523c126702a44307ef61cd0280659f0a78018 (diff)
downloadperlweeklychallenge-club-8e79725397c7bd8166530842cd3fdc3eb031cdca.tar.gz
perlweeklychallenge-club-8e79725397c7bd8166530842cd3fdc3eb031cdca.tar.bz2
perlweeklychallenge-club-8e79725397c7bd8166530842cd3fdc3eb031cdca.zip
Optimize
Diffstat (limited to 'challenge-191')
-rwxr-xr-xchallenge-191/wlmb/perl/ch-2a.pl12
-rwxr-xr-xchallenge-191/wlmb/perl/ch-2b.pl8
-rwxr-xr-xchallenge-191/wlmb/perl/ch-2c.pl31
3 files changed, 24 insertions, 27 deletions
diff --git a/challenge-191/wlmb/perl/ch-2a.pl b/challenge-191/wlmb/perl/ch-2a.pl
index 340ea1ecd0..19c49b2d48 100755
--- a/challenge-191/wlmb/perl/ch-2a.pl
+++ b/challenge-191/wlmb/perl/ch-2a.pl
@@ -11,19 +11,19 @@ sub cute($n){ # iterator over cute sequences
for my $position(0..$n-1){
for(1..$n){
push @{$sets[$position]}, $_ if ($position+1)%$_==0 || $_%($position+1)==0;
- }
+ }
}
my $iter=Set::CrossProduct->new([@sets]);
return sub {
- ITER: while(my $tuple=$iter->get()){
+ ITER: while(my $tuple=$iter->get()){
my @seen;
for(@$tuple){
- next ITER if $seen[$_];
+ next ITER if $seen[$_];
++$seen[$_];
}
- return $tuple;
- }
- return;
+ return $tuple;
+ }
+ return;
}
}
die << "EOF" unless @ARGV;
diff --git a/challenge-191/wlmb/perl/ch-2b.pl b/challenge-191/wlmb/perl/ch-2b.pl
index b0ae82e271..2e66f53809 100755
--- a/challenge-191/wlmb/perl/ch-2b.pl
+++ b/challenge-191/wlmb/perl/ch-2b.pl
@@ -11,7 +11,7 @@ sub cute($n){ # generate all cute sequences of length $n
for my $position(0..$n-1){
for(1..$n){ # Build sets of divisors and multiples of $position+1
push @{$sets[$position]}, $_ if ($position+1)%$_==0 || $_%($position+1)==0;
- }
+ }
}
my $x=reduce {cute_aux($a, $b)} [[]], @sets; # combine sets into cute sequences
return $x;
@@ -24,9 +24,9 @@ sub cute_aux($seqs, $nums){ # combine an ongoing set of cute sequences with a se
my $num=$tuple->[1];
my @seen;
map {$seen[$_]=1} @array; # Seen numbers
- next if $seen[$num]; # Throw away repetitions
- push @array, $num; # add number to current sequence
- push @combined, [@array]; # add sequence to set of ongoing sequences
+ next if $seen[$num]; # Throw away repetitions
+ push @array, $num; # add number to current sequence
+ push @combined, [@array]; # add sequence to set of ongoing sequences
}
return [@combined];
}
diff --git a/challenge-191/wlmb/perl/ch-2c.pl b/challenge-191/wlmb/perl/ch-2c.pl
index e212686d6a..ac2839648e 100755
--- a/challenge-191/wlmb/perl/ch-2c.pl
+++ b/challenge-191/wlmb/perl/ch-2c.pl
@@ -12,27 +12,24 @@ sub cute($n){ # generate all cute sequences of length $n
push @{$sets[$position]}, $_ if ($position+1)%$_==0 || $_%($position+1)==0;
}
}
- my $x=reduce {cute_aux($a, $b)} [[]], @sets; # combine sets into cute sequences
- return $x;
-}
-sub cute_aux($seqs, $nums){ # combine an ongoing set of cute sequences with a set of numbers
- my @combined =
- map {
- my $seq=$_;
+ my $x=reduce {
+ my @combined =
map {
- my @seq=@$seq;
- my @seen;
- map {$seen[$_]=1} @seq;
- push @seq, $_;
- $seen[$_]?():[@seq]
- } @$nums
- }
- @$seqs;
- [@combined];
+ my @seq=@$_;
+ map {
+ my @seen;
+ @seen[@seq]=(1)x@seq;
+ $seen[$_]?():[@seq, $_]
+ } @$b
+ }
+ @$a;
+ [@combined];
+ } [[]], @sets; # combine sets into cute sequences
+ return $x;
}
die << "EOF" unless @ARGV;
Usage: $0 N1 [N2...]
to count the cute orderings of 1..Ni
EOF
-die "Only numbers in the range 1..18 are allowed" unless all {1<=$_<=18} @ARGV;
+warn "Numbers beyond 18 will require patience" unless all {1<=$_<=18} @ARGV;
say "$_ -> ", scalar @{cute($_)} for(@ARGV);