aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-051/ryan-thompson/README.md9
-rw-r--r--challenge-051/ryan-thompson/blog.txt1
-rw-r--r--challenge-051/ryan-thompson/perl/ch-1.pl37
-rw-r--r--challenge-051/ryan-thompson/perl/ch-2.pl18
-rw-r--r--challenge-051/ryan-thompson/raku/ch-1.p624
-rw-r--r--challenge-051/ryan-thompson/raku/ch-2.p616
6 files changed, 100 insertions, 5 deletions
diff --git a/challenge-051/ryan-thompson/README.md b/challenge-051/ryan-thompson/README.md
index e79dd77e11..454d7be469 100644
--- a/challenge-051/ryan-thompson/README.md
+++ b/challenge-051/ryan-thompson/README.md
@@ -1,18 +1,17 @@
# Ryan Thompson
-## Week 050 Solutions
+## Week 051 Solutions
-### Task 1 › Merge Intervals
+### Task 1 › 3Sum
* [Perl](perl/ch-1.pl)
* [Raku](raku/ch-1.p6)
-### Task 2 › Noble Integers
+### Task 2 › Colourful Numbers
* [Perl](perl/ch-2.pl)
* [Raku](raku/ch-2.p6)
## Blogs
- * [Task 1 › Merge Intervals](http://www.ry.ca/2020/03/merge-intervals/)
- * [Task 2 › Noble Integers](http://www.ry.ca/2020/03/noble-integers/)
+ * [Week 051 › 3Sum and Colourful Numbers](http://www.ry.ca/2020/03/pwc-051/)
diff --git a/challenge-051/ryan-thompson/blog.txt b/challenge-051/ryan-thompson/blog.txt
new file mode 100644
index 0000000000..8e74b6e8dc
--- /dev/null
+++ b/challenge-051/ryan-thompson/blog.txt
@@ -0,0 +1 @@
+http://www.ry.ca/2020/03/pwc-051/
diff --git a/challenge-051/ryan-thompson/perl/ch-1.pl b/challenge-051/ryan-thompson/perl/ch-1.pl
new file mode 100644
index 0000000000..8e16bf01e4
--- /dev/null
+++ b/challenge-051/ryan-thompson/perl/ch-1.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/env perl
+#
+# ch-1.pl - 3 Sum
+#
+# 2020 Ryan Thompson <rjt@cpan.org>
+
+use 5.010;
+use warnings;
+use strict;
+no warnings 'uninitialized';
+
+sub sum3 {
+ my $T = shift;
+ # Pre-build hash of numbers greater than $y for O(1) lookups in inner loop
+ my @a = @_; my %Lh = map { shift @a => { map { $_ => 1 } @a } } 1..$#a;
+
+ my @r;
+ while (my $x = shift) {
+ $Lh{$_}{ $T-$x-$_ } and push @r, [$x, $_, $T-$x-$_] for @_;
+ }
+ @r;
+}
+
+use Test::More;
+
+is_deeply [sum3( -2 => -4, -1, 0, 3, 4 )], [[-4, -1, 3]];
+is_deeply [sum3( -1 => -6, -5, 0, 5 )], [[-6, 0, 5]];
+is_deeply [sum3( 0 => -6, -1, 0, 1, 2 )], [[-1, 0, 1]];
+is_deeply [sum3( 2 => -8, -5, -2, 3, 4, 5 )], [[-5, 3, 4]];
+is_deeply [sum3( -3 => -7, -6, -5, 0, 5, 6, 8 )], [[-6, -5, 8]];
+is_deeply [sum3( 0 => -10, -9, -5, -4, -2, -1, 1, 5 )], [[-4, -1, 5]];
+is_deeply [sum3( -6 => -19, -13, -11, -9, -2, -1, 0, 1, 3, 7, 8, 9, 10, 11, 14, 16 )], [
+ [-19, -1, 14], [-19, 3, 10], [-13, -9, 16], [-13, -2, 9], [-13, -1, 8],
+ [-13, 0, 7], [-11, -9, 14], [-11, -2, 7], [-9, 0, 3],
+];
+
+done_testing;
diff --git a/challenge-051/ryan-thompson/perl/ch-2.pl b/challenge-051/ryan-thompson/perl/ch-2.pl
new file mode 100644
index 0000000000..fd123f3e17
--- /dev/null
+++ b/challenge-051/ryan-thompson/perl/ch-2.pl
@@ -0,0 +1,18 @@
+#!/usr/bin/env perl
+
+# ch-2.p6 - Colourful numbers with 3 digits
+#
+# Ryan Thompson <rjt@cpan.org>
+
+use 5.010;
+use warnings;
+use strict;
+
+sub is_colourful3 {
+ my ($x, $y, $z) = split //, $_[0];
+ my %seen;
+ $seen{$_}++ and return 0 for $x, $y, $z, $x*$y, $y*$z, $x*$y*$z;
+ return 1;
+}
+
+say for grep is_colourful3($_), 111..987;
diff --git a/challenge-051/ryan-thompson/raku/ch-1.p6 b/challenge-051/ryan-thompson/raku/ch-1.p6
new file mode 100644
index 0000000000..c0916f45ac
--- /dev/null
+++ b/challenge-051/ryan-thompson/raku/ch-1.p6
@@ -0,0 +1,24 @@
+#!/usr/bin/env perl6
+
+# ch-1.p6 - 3 Sum
+#
+# Ryan Thompson <rjt@cpan.org>
+
+sub sum3( Int $T, *@L ) {
+ @L.combinations(3).grep( $T == *.sum )
+}
+
+use Test;
+
+is-deeply [sum3( 0, -25, -10, -7, -3, 2, 4, 8, 10 )],
+ [ $(-10, 2, 8), $(-7, -3, 10) ];
+is-deeply [sum3( -2, -4, -1, 0, 3, 4 )], [ $(-4, -1, 3) ];
+is-deeply [sum3( -1, -6, -5, 0, 5 )], [ $(-6, 0, 5) ];
+is-deeply [sum3( 0, -6, -1, 0, 1, 2 )], [ $(-1, 0, 1) ];
+is-deeply [sum3( 2, -8, -5, -2, 3, 4, 5 )], [ $(-5, 3, 4) ];
+is-deeply [sum3( -3, -7, -6, -5, 0, 5, 6, 8 )], [ $(-6, -5, 8) ];
+is-deeply [sum3( 0, -10, -9, -5, -4, -2, -1, 1, 5 )], [ $(-4, -1, 5) ];
+is-deeply [sum3( -4, -11, -7, -6, -5, 2, 3, 4, 5, 6, 9 )],
+ [ $(-11, 2, 5), $(-11, 3, 4), $(-7, -6, 9) ];
+is-deeply [sum3( 1, -10, -5, -3, 0, 6, 7, 8, 9, 11 )],
+ [ $(-10, 0, 11), $(-5, -3, 9), $(-5, 0, 6) ];
diff --git a/challenge-051/ryan-thompson/raku/ch-2.p6 b/challenge-051/ryan-thompson/raku/ch-2.p6
new file mode 100644
index 0000000000..913fe32007
--- /dev/null
+++ b/challenge-051/ryan-thompson/raku/ch-2.p6
@@ -0,0 +1,16 @@
+#!/usr/bin/env perl6
+
+# ch-2.p6 - Colourful numbers of 3 digits
+#
+# Ryan Thompson <rjt@cpan.org>
+
+# This *almost* works, but combinations includes (first,last)
+#$n.comb».Int.combinations(1..3)».reduce({$^a * $^b}).unique.elems == 7;
+
+#| Returns True if a 3-digit number is colourful
+sub colourful( Int $n where { 100 ≤ $n ≤ 999 } --> Bool ) {
+ my @D = $n.comb».Int;
+ !([0], [1], [2], [0,1], [1,2], [0,1,2]).map({ [*] @D[$_] }).repeated;
+}
+
+.say for (100..999).grep: &colourful;