aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.freedom.nl>2021-12-20 19:06:27 +0100
committerAbigail <abigail@abigail.freedom.nl>2021-12-20 19:06:27 +0100
commit17f5562a76a8c54ea21fde11945f80ab697aed23 (patch)
tree262ab90cf36ab73772c561cc1eb7d1157abcc62d
parent8254e0cdd38e99a76e12136123400a6160d6f160 (diff)
downloadperlweeklychallenge-club-17f5562a76a8c54ea21fde11945f80ab697aed23.tar.gz
perlweeklychallenge-club-17f5562a76a8c54ea21fde11945f80ab697aed23.tar.bz2
perlweeklychallenge-club-17f5562a76a8c54ea21fde11945f80ab697aed23.zip
Perl solution for week 144, part 2
-rw-r--r--challenge-144/abigail/perl/ch-2.pl50
-rw-r--r--challenge-144/abigail/t/ctest.ini1
-rw-r--r--challenge-144/abigail/t/input-2-13
-rw-r--r--challenge-144/abigail/t/output-2-1.exp3
4 files changed, 57 insertions, 0 deletions
diff --git a/challenge-144/abigail/perl/ch-2.pl b/challenge-144/abigail/perl/ch-2.pl
new file mode 100644
index 0000000000..c682d7a525
--- /dev/null
+++ b/challenge-144/abigail/perl/ch-2.pl
@@ -0,0 +1,50 @@
+#!/opt/perl/bin/perl
+
+use 5.032;
+
+use strict;
+use warnings;
+no warnings 'syntax';
+
+use experimental 'signatures';
+use experimental 'lexical_subs';
+
+use List::Util qw [min];
+
+#
+# See ../README.md
+#
+
+#
+# Run as: perl ch-2.pl < input-file
+#
+
+sub ulam ($u1, $u2) {
+ die unless $u1 > 0 && $u2 > 0 && $u1 != $u2
+ && $u1 == int $u1 && $u2 == int $u2;
+ ($u1, $u2) = ($u2, $u1) if $u1 > $u2;
+
+ my @seen = ($u1, $u2);
+ my %sums;
+ $sums {$u1 + $u2} = 1;
+ my %todo = ($u1 + $u2, 1);
+
+ while (%todo) {
+ my $next = min keys %todo;
+ delete $todo {$next};
+ next if $sums {$next} > 1;
+ push @seen => $next;
+ return @seen if @seen >= 10;
+
+ foreach my $seen (@seen) {
+ next if $seen == $next;
+ my $sum = $seen + $next;
+ $sums {$sum} ++;
+ $todo {$sum} = 1;
+ }
+ }
+}
+
+$, = ", ";
+say ulam /[0-9]+/g while <>;
+
diff --git a/challenge-144/abigail/t/ctest.ini b/challenge-144/abigail/t/ctest.ini
index 88e8105056..d891360049 100644
--- a/challenge-144/abigail/t/ctest.ini
+++ b/challenge-144/abigail/t/ctest.ini
@@ -5,6 +5,7 @@
[names]
1-1 = Fixed output
+2-1 = Given Examples
[1-1]
no_input = 1
diff --git a/challenge-144/abigail/t/input-2-1 b/challenge-144/abigail/t/input-2-1
new file mode 100644
index 0000000000..61becdbf4a
--- /dev/null
+++ b/challenge-144/abigail/t/input-2-1
@@ -0,0 +1,3 @@
+1 2
+2 3
+2 5
diff --git a/challenge-144/abigail/t/output-2-1.exp b/challenge-144/abigail/t/output-2-1.exp
new file mode 100644
index 0000000000..97b1a800ee
--- /dev/null
+++ b/challenge-144/abigail/t/output-2-1.exp
@@ -0,0 +1,3 @@
+1, 2, 3, 4, 6, 8, 11, 13, 16, 18
+2, 3, 5, 7, 8, 9, 13, 14, 18, 19
+2, 5, 7, 9, 11, 12, 13, 15, 19, 23