aboutsummaryrefslogtreecommitdiff
path: root/challenge-030
diff options
context:
space:
mode:
authorJoelle Maslak <jmaslak@antelope.net>2019-10-19 14:04:51 -0600
committerJoelle Maslak <jmaslak@antelope.net>2019-10-19 14:04:51 -0600
commit550b41b517d32a662b05f1067384502fa46fe6d0 (patch)
tree2313b516366bd34730e7a0a56252fdc6c5d5a5f9 /challenge-030
parent45beb0d934d241d1f90e7c9c5ebe1a6baf7e05c1 (diff)
downloadperlweeklychallenge-club-550b41b517d32a662b05f1067384502fa46fe6d0.tar.gz
perlweeklychallenge-club-550b41b517d32a662b05f1067384502fa46fe6d0.tar.bz2
perlweeklychallenge-club-550b41b517d32a662b05f1067384502fa46fe6d0.zip
Joelle's Solutions for Week 30 in Raku and Perl 5.
Diffstat (limited to 'challenge-030')
-rwxr-xr-xchallenge-030/joelle-maslak/perl5/ch-1.pl13
-rwxr-xr-xchallenge-030/joelle-maslak/perl5/ch-2.pl91
-rwxr-xr-xchallenge-030/joelle-maslak/perl6/ch-1.p611
-rwxr-xr-xchallenge-030/joelle-maslak/perl6/ch-2.p653
4 files changed, 168 insertions, 0 deletions
diff --git a/challenge-030/joelle-maslak/perl5/ch-1.pl b/challenge-030/joelle-maslak/perl5/ch-1.pl
new file mode 100755
index 0000000000..ab390141be
--- /dev/null
+++ b/challenge-030/joelle-maslak/perl5/ch-1.pl
@@ -0,0 +1,13 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+
+use v5.22;
+
+use POSIX qw(mktime strftime);
+
+say join "\n", map { strftime( "%Y-%m-%d", gmtime($_) ) }
+ grep { ( gmtime($_) )[6] == 0 }
+ map { mktime( 0, 0, 0, 25, 11, $_ - 1900 ) } 2020 .. 2100;
+
diff --git a/challenge-030/joelle-maslak/perl5/ch-2.pl b/challenge-030/joelle-maslak/perl5/ch-2.pl
new file mode 100755
index 0000000000..8d31d74053
--- /dev/null
+++ b/challenge-030/joelle-maslak/perl5/ch-2.pl
@@ -0,0 +1,91 @@
+#!/usr/bin/env perl
+use v5.26;
+use strict;
+use warnings;
+
+# Turn on method signatures
+use feature 'signatures';
+no warnings 'experimental::signatures';
+
+# Turn on post-deref
+use feature 'postderef';
+no warnings 'experimental::postderef';
+
+# Turn on lexical subroutines
+use feature 'lexical_subs';
+no warnings 'experimental::lexical_subs';
+
+use List::Util qw(sum);
+
+# Problem:
+# Write a script to print all possible series of 3 positive numbers,
+# where in each series at least one of the numbers is even and the sum
+# of the three numbers is always 12. For example, (3, 4, 5)
+#
+# This description uses the term "series" but does not seem to use this
+# word in a typical mathematical way. I am assuming that what is being
+# asked for is actually *sets* of numbers, where the following is true:
+#
+# Being a set, no ordering is implied. Thus 1,2,3 is the same as 3,2,1,
+# and thus it should only be output once.
+#
+# Being a set, no duplicates are allowed. I.E. 4,4,4 is not valid.
+#
+# Because positive numbers are defined, zero is not valid.
+#
+# I'm assuming only integers are being requested, even though that is
+# not explictly stated. Otherwise this would be an infinite sequence.
+#
+# Because we must have 3 numbers which are ≥ 1, and we know 1+2+3 (the
+# smallest 3 numbers that are ≥ 1) is not 12, we can assume n+1+2
+# might equal 12 for some n > 2, and this would be the largest value
+# that could be added to any other two positive integers that add up
+# to 12. I.E. 12-3 = 9, so 1+2+9 = 12, so we know the set must not
+# contain numbers that are not between (inclusively) 1 and 9.
+#
+# Finally, we are told at least one of the numbers is even. This
+# means no more than 2 numbers are odd. We know from number theory,
+# that adding 3 positive, odd numbers would produce an odd number
+# (I.E. two positive, odd numbers added always produces an even number,
+# and if that even number was added with an odd number, the result
+# would be odd - since 12 is definitely not odd, we know that three
+# positive, odd numbers are not a possibility).
+#
+
+# We will do this with a pipeline.
+
+# Read this next portion of code from bottom up.
+say join "\n", # Build a string, seperating sets by newlines
+ map { join( ",", $_->@* ) } # Make the sets into comma-deliminated strings
+ grep { sum( $_->@* ) == 12 } # Make sure these numbers sum to 12
+ grep { $_->[1] < $_->[2] } # Make sure last number is the biggest
+ grep { $_->[0] < $_->[1] } # Make sure 2nd number is bigger than 1st
+ cross( [ 1 .. 7 ], [ 2 .. 8 ], [ 3 .. 9 ] ); # Create a list where each element is a list
+ # of 3 numbers.
+
+# Cross product of multiple lists
+# Takes multiple (2+) list references. Returns a list of list
+# references.
+sub cross(@lists) {
+ confess("Invalid call") if scalar(@lists) == 0;
+
+ my sub recursive_cross ( $output, @lists ) {
+ return $output->@* if scalar(@lists) == 0;
+
+ my (@addlist) = ( shift @lists )->@*;
+
+ # A lexical sub in a lexical sub? Why not.
+ my sub append(@l) {
+ return map { [ @l, $_ ] } @addlist;
+ }
+
+ # Note the map on the next line uses the expression form...
+ my (@result) = map append( $_->@* ), $output->@*;
+
+ return __SUB__->( \@result, @lists );
+ }
+
+ my (@output) = map { [$_] } ( shift @lists )->@*;
+ return recursive_cross( \@output, @lists );
+}
+
diff --git a/challenge-030/joelle-maslak/perl6/ch-1.p6 b/challenge-030/joelle-maslak/perl6/ch-1.p6
new file mode 100755
index 0000000000..3c9b8211cb
--- /dev/null
+++ b/challenge-030/joelle-maslak/perl6/ch-1.p6
@@ -0,0 +1,11 @@
+#!/usr/bin/env perl6
+use v6;
+
+sub MAIN(:$start = 2019, :$end = 2100) {
+ my $christmasses = ($start..$end).map({ DateTime.new(:25day, :12month, :year($_)) });
+
+ my $on-sunday = $christmasses.grep: *.day-of-week == 7;
+
+ say $on-sunday».yyyy-mm-dd.join("\n");
+}
+
diff --git a/challenge-030/joelle-maslak/perl6/ch-2.p6 b/challenge-030/joelle-maslak/perl6/ch-2.p6
new file mode 100755
index 0000000000..67d2299ee4
--- /dev/null
+++ b/challenge-030/joelle-maslak/perl6/ch-2.p6
@@ -0,0 +1,53 @@
+#!/usr/bin/env perl6
+use v6;
+
+# Problem:
+# Write a script to print all possible series of 3 positive numbers,
+# where in each series at least one of the numbers is even and the sum
+# of the three numbers is always 12. For example, (3, 4, 5)
+#
+# This description uses the term "series" but does not seem to use this
+# word in a typical mathematical way. I am assuming that what is being
+# asked for is actually *sets* of numbers, where the following is true:
+#
+# Being a set, no ordering is implied. Thus 1,2,3 is the same as 3,2,1,
+# and thus it should only be output once.
+#
+# Being a set, no duplicates are allowed. I.E. 4,4,4 is not valid.
+#
+# Because positive numbers are defined, zero is not valid.
+#
+# I'm assuming only integers are being requested, even though that is
+# not explictly stated. Otherwise this would be an infinite sequence.
+#
+# Because we must have 3 numbers which are ≥ 1, and we know 1+2+3 (the
+# smallest 3 numbers that are ≥ 1) is not 12, we can assume n+1+2
+# might equal 12 for some n > 2, and this would be the largest value
+# that could be added to any other two positive integers that add up
+# to 12. I.E. 12-3 = 9, so 1+2+9 = 12, so we know the set must not
+# contain numbers that are not between (inclusively) 1 and 9.
+#
+# Finally, we are told at least one of the numbers is even. This
+# means no more than 2 numbers are odd. We know from number theory,
+# that adding 3 positive, odd numbers would produce an odd number
+# (I.E. two positive, odd numbers added always produces an even number,
+# and if that even number was added with an odd number, the result
+# would be odd - since 12 is definitely not odd, we know that three
+# positive, odd numbers are not a possibility).
+#
+
+# We will do this with a pipeline.
+
+cross((1..9) xx 3)\ # Create a list where each element is a list of
+ # 3 numbers inclusively between 1 and 9.
+ ».Set # Turn those elements into sets
+ .grep( *.elems == 3 ) # Sets flatten duplicates, so we want to be sure
+ # there are still 3 elements in the set.
+ .unique # We will have duplicate sets, remove the dupes
+ .map( *.keys.list ) # Turn the list back into a list
+ .grep( *.sum == 12 ) # Do the set members add to 12?
+ .map( *.sort ) # Sort the numbers in each set
+ .map( *.join(",") ) # Make the sets into comma-deliminated strings
+ .sort # Sort the sets of numbers
+ .join("\n") # Build a string, seperating sets by newlines
+ .say