aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuis Mochan <mochan@fis.unam.mx>2022-11-15 19:50:41 -0600
committerLuis Mochan <mochan@fis.unam.mx>2022-11-15 19:50:41 -0600
commitec06787106a2125f474df6132cc251097042083b (patch)
treeb48f681c24578d9281d34c2c5b4b98fb56047c43
parentd66a2ec94b58de6926d7659223f8a4a1d2acdcca (diff)
downloadperlweeklychallenge-club-ec06787106a2125f474df6132cc251097042083b.tar.gz
perlweeklychallenge-club-ec06787106a2125f474df6132cc251097042083b.tar.bz2
perlweeklychallenge-club-ec06787106a2125f474df6132cc251097042083b.zip
Add more economical solutions
-rwxr-xr-xchallenge-191/wlmb/perl/ch-2a.pl39
-rwxr-xr-xchallenge-191/wlmb/perl/ch-2b.pl38
2 files changed, 77 insertions, 0 deletions
diff --git a/challenge-191/wlmb/perl/ch-2a.pl b/challenge-191/wlmb/perl/ch-2a.pl
new file mode 100755
index 0000000000..340ea1ecd0
--- /dev/null
+++ b/challenge-191/wlmb/perl/ch-2a.pl
@@ -0,0 +1,39 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 191
+# Task 2: Cute List. Economize memory
+#
+# See https://wlmb.github.io/2022/11/14/PWC191/#task-2-cute-list
+use v5.36;
+use List::Util qw(all any);
+use Set::CrossProduct;
+sub cute($n){ # iterator over cute sequences
+ my @sets;
+ 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()){
+ my @seen;
+ for(@$tuple){
+ next ITER if $seen[$_];
+ ++$seen[$_];
+ }
+ return $tuple;
+ }
+ return;
+ }
+}
+die << "EOF" unless @ARGV;
+Usage: $0 N1 [N2...]
+to count the cute orderings of 1..Ni
+EOF
+die "Only numbers in the range 1..15 are allowed" unless all {1<=$_<=15} @ARGV;
+for(@ARGV){
+ my $iter=cute($_);
+ my $count=0;
+ ++$count while $iter->();
+ say "$_ -> $count";
+}
diff --git a/challenge-191/wlmb/perl/ch-2b.pl b/challenge-191/wlmb/perl/ch-2b.pl
new file mode 100755
index 0000000000..b0ae82e271
--- /dev/null
+++ b/challenge-191/wlmb/perl/ch-2b.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/env perl
+# Perl weekly challenge 191
+# Task 2: Cute List. Throw duplicates early.
+#
+# See https://wlmb.github.io/2022/11/14/PWC191/#task-2-cute-list
+use v5.36;
+use Set::CrossProduct;
+use List::Util qw(all reduce);
+sub cute($n){ # generate all cute sequences of length $n
+ my @sets;
+ 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;
+}
+sub cute_aux($seqs, $nums){ # combine an ongoing set of cute sequences with a set of numbers
+ my $iter=Set::CrossProduct->new([$seqs, $nums]);
+ my @combined;
+ while(my $tuple=$iter->get){ # Cartesian product of a sequence and a number
+ my @array=@{$tuple->[0]};
+ 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
+ }
+ return [@combined];
+}
+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;
+say "$_ -> ", scalar @{cute($_)} for(@ARGV);