aboutsummaryrefslogtreecommitdiff
path: root/challenge-014
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-06-28 19:17:13 +0100
committerGitHub <noreply@github.com>2019-06-28 19:17:13 +0100
commitef2f942eecc7aa496bb7dff3af2bde520ebdc540 (patch)
treee1e46cba2debd380807a79b2ae7311ff9e601f45 /challenge-014
parent5c8e007af38a211475dcf076f78a6aaeeec8c140 (diff)
parenta47346af244db4042477f0ee9b94bdc6f31a136b (diff)
downloadperlweeklychallenge-club-ef2f942eecc7aa496bb7dff3af2bde520ebdc540.tar.gz
perlweeklychallenge-club-ef2f942eecc7aa496bb7dff3af2bde520ebdc540.tar.bz2
perlweeklychallenge-club-ef2f942eecc7aa496bb7dff3af2bde520ebdc540.zip
Merge pull request #313 from jmaslak/joelle-14-2-1
Solution to 14.1 in P6 and P5
Diffstat (limited to 'challenge-014')
-rwxr-xr-xchallenge-014/joelle-maslak/perl5/ch-1.pl50
-rwxr-xr-xchallenge-014/joelle-maslak/perl6/ch-1.p644
2 files changed, 94 insertions, 0 deletions
diff --git a/challenge-014/joelle-maslak/perl5/ch-1.pl b/challenge-014/joelle-maslak/perl5/ch-1.pl
new file mode 100755
index 0000000000..583a8d3956
--- /dev/null
+++ b/challenge-014/joelle-maslak/perl5/ch-1.pl
@@ -0,0 +1,50 @@
+#!/usr/bin/env perl
+use v5.22;
+use strict;
+use warnings;
+
+# Turn on method signatures
+use feature 'signatures';
+no warnings 'experimental::signatures';
+
+use autodie;
+
+die("Usage: $0 <number of sequence elements>") unless @ARGV <= 1;
+my $length = $ARGV[0] // 19;
+
+sub MAIN() {
+ say join ' ', map { van_eck($_) } 0..($length-1);
+}
+
+my %cache_by_val = (0 => [0, 1]);
+my @cache_by_pos = (0, 0);
+
+sub van_eck($pos) {
+ return 0 if $pos <= 1;
+
+ return $cache_by_pos[$pos] if exists $cache_by_pos[$pos];
+
+ # Ensure cache is populated
+ for (my $p=0; $p<$pos; $p++) { van_eck($p) }
+
+ my $n = $pos - 1;
+
+ # We know that we must have been called in order.
+ my $prev = van_eck($n);
+
+ my $current;
+ if (exists $cache_by_val{$prev} and scalar($cache_by_val{$prev}->@*) >= 2) {
+ my $m = $cache_by_val{$prev}->[-2];
+ $current = $n - $m;
+ } else {
+ $current = 0;
+ }
+ $cache_by_pos[$pos] = $current;
+ $cache_by_val{$current} = [] unless exists $cache_by_val{$current};
+ push $cache_by_val{$current}->@*, $pos;
+
+ return $current;
+}
+
+MAIN();
+
diff --git a/challenge-014/joelle-maslak/perl6/ch-1.p6 b/challenge-014/joelle-maslak/perl6/ch-1.p6
new file mode 100755
index 0000000000..05f3653422
--- /dev/null
+++ b/challenge-014/joelle-maslak/perl6/ch-1.p6
@@ -0,0 +1,44 @@
+#!/usr/bin/env perl6
+use v6;
+
+#
+# Copyright © 2019 Joelle Maslak
+# All Rights Reserved - See License
+#
+
+sub MAIN(UInt:D $length = 19) {
+ my $seq = lazy gather { for 0..∞ -> $n { take van-eck($n) } }
+
+ say $seq[^$length].join(" ");
+}
+
+my %cache-by-val = 0 => [0, 1];
+my @cache-by-pos = 0, 0;
+
+multi sub van-eck(0 --> UInt:D) { 0 }
+multi sub van-eck(1 --> UInt:D) { 0 }
+multi sub van-eck(UInt:D $pos --> UInt:D) {
+
+ return @cache-by-pos[$pos] if @cache-by-pos[$pos]:exists;
+
+ # Ensure cache is populated.
+ for ^$pos -> $p { van-eck($p) }
+
+ my $n = $pos - 1;
+
+ # We know that we must have been called in order.
+ my $prev = van-eck($n);
+
+ my $current;
+ if %cache-by-val{$prev} and %cache-by-val{$prev}.elems ≥ 2 {
+ my $m = %cache-by-val{$prev}[*-2];
+ $current = $n - $m;
+ } else {
+ $current = 0;
+ }
+ @cache-by-pos[$pos] = $current;
+ ( %cache-by-val{$current} //= Array.new ).push: $pos;
+
+ return $current;
+}
+