aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-119/perlboy1967/perl/ch-1.pl35
-rwxr-xr-xchallenge-119/perlboy1967/perl/ch-2.pl54
2 files changed, 89 insertions, 0 deletions
diff --git a/challenge-119/perlboy1967/perl/ch-1.pl b/challenge-119/perlboy1967/perl/ch-1.pl
new file mode 100755
index 0000000000..933f0fb820
--- /dev/null
+++ b/challenge-119/perlboy1967/perl/ch-1.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/perl
+
+# Perl Weekly Challenge - 119
+# - https://perlweeklychallenge.org/blog/perl-weekly-challenge-119/#TASK1
+#
+# Task 1 - Swap Nibbles
+#
+# Author: Niels 'PerlBoy' van Dijke
+
+use v5.16;
+use strict;
+use warnings;
+
+use Test::More;
+
+# Prototype(s)
+sub swapNibble($);
+
+my $tests = [
+ [101, 86],
+ [ 18, 33],
+ [123, 183],
+ [136, 136],
+];
+
+foreach my $t (@$tests) {
+ printf "%s\n", join(',', @$t) unless is(swapNibble($t->[0]), $t->[1]);
+}
+
+done_testing();
+
+sub swapNibble($) {
+ return (($_[0] & 0x0f) << 4) +
+ (($_[0] & 0xf0) >> 4);
+}
diff --git a/challenge-119/perlboy1967/perl/ch-2.pl b/challenge-119/perlboy1967/perl/ch-2.pl
new file mode 100755
index 0000000000..0427ef62aa
--- /dev/null
+++ b/challenge-119/perlboy1967/perl/ch-2.pl
@@ -0,0 +1,54 @@
+#!/usr/bin/perl
+
+# Perl Weekly Challenge - 119
+# - https://perlweeklychallenge.org/blog/perl-weekly-challenge-119/#TASK1
+#
+# Task 2 - Sequence without 1-on-1
+#
+# Author: Niels 'PerlBoy' van Dijke
+#
+
+use v5.16;
+use strict;
+use warnings;
+
+# Prototype(s)
+sub close2Base3Seq($);
+
+use Test::More;
+
+my $tests = [
+ [ 1, 1],
+ [ 4, 12],
+ [ 8, 23],
+ [ 9, 31],
+ [ 15, 131],
+ [ 60,2223],
+];
+
+foreach my $t (@$tests) {
+ printf "%s\n", join(',', @$t) unless is(close2Base3Seq($t->[0]), $t->[1]);
+}
+
+done_testing();
+
+
+sub close2Base3Seq($) {
+ my ($n) = @_;
+
+ state @cache; $cache[1] //= 1;
+
+ # Try our luck using our cache
+ return $cache[$n] if defined $cache[$n];
+
+ my $maxN = scalar(@cache) - 1;
+
+ while ($maxN < $n) {
+ my $seqValue = $cache[$maxN++]+1;
+ 1 while ($seqValue =~ s#(.*)4#("0$1"+1).'1'#e or $seqValue =~ s#11#12#);
+ $cache[$maxN] = $seqValue;
+ }
+
+ return $cache[$n];
+}
+