aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Thompson <i@ry.ca>2020-02-22 05:09:03 -0600
committerRyan Thompson <i@ry.ca>2020-02-22 05:09:03 -0600
commitdcf0f13cf3843d6bd75533ba00a93994709c55be (patch)
treebcc32cd6c524c04f67f3d52242ade25a1296a136
parent49ac62fb8c77fac685a2e1ab5d04e8dac44bf8dd (diff)
downloadperlweeklychallenge-club-dcf0f13cf3843d6bd75533ba00a93994709c55be.tar.gz
perlweeklychallenge-club-dcf0f13cf3843d6bd75533ba00a93994709c55be.tar.bz2
perlweeklychallenge-club-dcf0f13cf3843d6bd75533ba00a93994709c55be.zip
rjt's Week 048 solutions and blogs
-rw-r--r--challenge-048/ryan-thompson/README.md12
-rw-r--r--challenge-048/ryan-thompson/blog.txt1
-rw-r--r--challenge-048/ryan-thompson/blog1.txt1
-rw-r--r--challenge-048/ryan-thompson/perl/ch-1.pl43
-rw-r--r--challenge-048/ryan-thompson/perl/ch-2.pl19
-rw-r--r--challenge-048/ryan-thompson/raku/ch-1.p614
-rw-r--r--challenge-048/ryan-thompson/raku/ch-2.p616
7 files changed, 100 insertions, 6 deletions
diff --git a/challenge-048/ryan-thompson/README.md b/challenge-048/ryan-thompson/README.md
index 4994e028ce..9d5c2b26a1 100644
--- a/challenge-048/ryan-thompson/README.md
+++ b/challenge-048/ryan-thompson/README.md
@@ -1,18 +1,18 @@
# Ryan Thompson
-## Week 047 Solutions
+## Week 048 Solutions
-### Task 1 › Roman Calculator
+### Task 1 › Survivor
* [Perl](perl/ch-1.pl)
- * **Raku:** No Raku solution this week, sorry.
+ * [Raku](raku/ch-1.p6)
-### Task 2 › Gapful Numbers
+### Task 2 › Palindrome Dates
* [Perl](perl/ch-2.pl)
* [Raku](raku/ch-2.p6)
## Blogs
- * [Task 1 › Roman Calculator](http://www.ry.ca/2020/02/roman-calculator/)
- * [Task 2 › Gapful Numbers](http://www.ry.ca/2020/02/gapful-numbers/)
+ * [Task 1 › Survivor](http://www.ry.ca/2020/02/survivor-josepheus-problem/)
+ * [Task 2 › Palindrome Dates](http://www.ry.ca/2020/02/palindrome-dates/)
diff --git a/challenge-048/ryan-thompson/blog.txt b/challenge-048/ryan-thompson/blog.txt
new file mode 100644
index 0000000000..9282750ac8
--- /dev/null
+++ b/challenge-048/ryan-thompson/blog.txt
@@ -0,0 +1 @@
+http://www.ry.ca/2020/02/survivor-josepheus-problem/
diff --git a/challenge-048/ryan-thompson/blog1.txt b/challenge-048/ryan-thompson/blog1.txt
new file mode 100644
index 0000000000..071c46fc8c
--- /dev/null
+++ b/challenge-048/ryan-thompson/blog1.txt
@@ -0,0 +1 @@
+http://www.ry.ca/2020/02/palindrome-dates/
diff --git a/challenge-048/ryan-thompson/perl/ch-1.pl b/challenge-048/ryan-thompson/perl/ch-1.pl
new file mode 100644
index 0000000000..9ba6e8a8f6
--- /dev/null
+++ b/challenge-048/ryan-thompson/perl/ch-1.pl
@@ -0,0 +1,43 @@
+#!/usr/bin/env perl
+#
+# ch-1.pl - Survivor
+#
+# Ryan Thompson <rjt@cpan.org>
+
+use 5.010;
+use warnings;
+use strict;
+
+say survivor(shift) and exit if @ARGV;
+
+# Linked list solution
+sub survivor {
+ my $N = shift;
+ my @ll = (undef, 2..$N, 1); # Circular linked list
+ my $cur = 1;
+ $ll[$cur] = $ll[$ll[$cur]], $cur = $ll[$cur] until $ll[$cur] == $cur;
+
+ $cur;
+}
+
+sub analytic {
+ my $N = shift;
+ 2 * ($N - 2**int( log($N) / log(2) )) + 1;
+}
+
+#
+# Benchmarking and Tests
+#
+
+use Benchmark qw/cmpthese/;
+use Test::More;
+
+cmpthese(-5, {
+ linked => sub { survivor($_) for 1..100 },
+ analytic => sub { analytic($_) for 1..100 },
+});
+
+is survivor(100), 73;
+is survivor(50), 37;
+is survivor($_), analytic($_) for 1..100;
+done_testing;
diff --git a/challenge-048/ryan-thompson/perl/ch-2.pl b/challenge-048/ryan-thompson/perl/ch-2.pl
new file mode 100644
index 0000000000..74b2ef0bb8
--- /dev/null
+++ b/challenge-048/ryan-thompson/perl/ch-2.pl
@@ -0,0 +1,19 @@
+#!/usr/bin/env perl
+#
+# ch-2.pl - Palindrome Dates (mm/dd/yyyy)
+#
+# Ryan Thompson <rjt@cpan.org>
+
+use warnings;
+use strict;
+
+# All of the 2-digit years must also be months
+my @yy = sort map { chop . ($_||0) } 1..12;
+
+# Ensure printing in the correct order, with minimal looping
+for my $dd (qw<02 12 22>) {
+ for my $yy (@yy) {
+ printf "%02d-%02d-%02d%02d\n",
+ scalar reverse($yy), $dd, scalar reverse($dd), $yy;
+ }
+}
diff --git a/challenge-048/ryan-thompson/raku/ch-1.p6 b/challenge-048/ryan-thompson/raku/ch-1.p6
new file mode 100644
index 0000000000..7999a85fdc
--- /dev/null
+++ b/challenge-048/ryan-thompson/raku/ch-1.p6
@@ -0,0 +1,14 @@
+#!/usr/bin/env perl6
+
+# ch-1.p6 - Survivor
+#
+# Ryan Thompson <rjt@cpan.org>
+
+sub MAIN( Int $N = 50 ) {
+ my Int @ll = 0, |[1..$N].rotate;
+ my Int $cur = 1;
+
+ @ll[$cur] = @ll[ @ll[$cur] ] and $cur = @ll[$cur] until @ll[$cur] == $cur;
+
+ say $cur;
+}
diff --git a/challenge-048/ryan-thompson/raku/ch-2.p6 b/challenge-048/ryan-thompson/raku/ch-2.p6
new file mode 100644
index 0000000000..90420538f0
--- /dev/null
+++ b/challenge-048/ryan-thompson/raku/ch-2.p6
@@ -0,0 +1,16 @@
+#!/usr/bin/env perl6
+
+# ch-2.p6 - Palindrome Dates (mm/dd/yyyy)
+#
+# Ryan Thompson <rjt@cpan.org>
+
+for (<02 12 22> X (1..12)».fmt('%02d')».flip.sort).flat -> $dd, $yy {
+ say "{$yy.flip}-$dd-{$dd.flip}$yy";
+}
+
+# Naive method
+#.say for (2000..2999).map({ $_.flip.comb(2), $_ }).flat.grep: &valid-date;
+
+#| Return True if a mmddyyyy date is valid
+# This won't work in general; it works here because of the range (see blog)
+#sub valid-date( $m, $d, $y ) { 1 ≤ $m ≤ 12 and 1 ≤ $d ≤ 31; }