aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Jacoby <jacoby.david@gmail.com>2024-03-18 13:13:31 -0400
committerDave Jacoby <jacoby.david@gmail.com>2024-03-18 13:13:31 -0400
commit9e7e80939c8a0ae12ace548d7b9efb43b32ec4c3 (patch)
tree8709460cb59b9eaab464d094491b8b6b2b2ffaaf
parentcad939ed2335566e31b26c5b74b9fc99c23db7a5 (diff)
downloadperlweeklychallenge-club-9e7e80939c8a0ae12ace548d7b9efb43b32ec4c3.tar.gz
perlweeklychallenge-club-9e7e80939c8a0ae12ace548d7b9efb43b32ec4c3.tar.bz2
perlweeklychallenge-club-9e7e80939c8a0ae12ace548d7b9efb43b32ec4c3.zip
DAJ 261
-rw-r--r--challenge-261/dave-jacoby/blog.txt1
-rw-r--r--challenge-261/dave-jacoby/perl/ch-1.pl32
-rw-r--r--challenge-261/dave-jacoby/perl/ch-2.pl33
3 files changed, 66 insertions, 0 deletions
diff --git a/challenge-261/dave-jacoby/blog.txt b/challenge-261/dave-jacoby/blog.txt
new file mode 100644
index 0000000000..22c32d2a3a
--- /dev/null
+++ b/challenge-261/dave-jacoby/blog.txt
@@ -0,0 +1 @@
+https://jacoby-lpwk.onrender.com/2024/03/18/can-you-digit-it-weekly-challenge-261.html
diff --git a/challenge-261/dave-jacoby/perl/ch-1.pl b/challenge-261/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..77fb850a02
--- /dev/null
+++ b/challenge-261/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ say postderef signatures state };
+
+use List::Util qw{ sum0 };
+
+my @examples = (
+
+ [ 1, 2, 3, 45 ],
+ [ 1, 12, 3 ],
+ [ 1, 2, 3, 4 ],
+ [ 236, 416, 336, 350 ],
+);
+
+for my $example (@examples) {
+ my @ints = $example->@*;
+ my $ints = join ',', @ints;
+ my $output = element_digit_sum(@ints);
+ say <<"END";
+ Input: \@ints = ($ints)
+ Output: $output
+END
+}
+
+sub element_digit_sum (@ints) {
+ my @digits = map { split //, $_ } @ints;
+ my $element_sum = sum0 @ints;
+ my $digit_sum = sum0 @digits;
+ return abs $element_sum - $digit_sum;
+}
diff --git a/challenge-261/dave-jacoby/perl/ch-2.pl b/challenge-261/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..82b437a08c
--- /dev/null
+++ b/challenge-261/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ say postderef signatures state };
+
+use List::Util qw{ any };
+
+my @examples = (
+
+ { ints => [ 5, 3, 6, 1, 12 ], start => 3 },
+ { ints => [ 1, 2, 4, 3 ], start => 1 },
+ { ints => [ 5, 6, 7 ], start => 2 },
+);
+
+for my $example (@examples) {
+ my $start = $example->{start};
+ my @ints = $example->{ints}->@*;
+ my $output = multiply_by_two( $start, @ints );
+ my $ints = join ',', @ints;
+
+ say <<"END";
+ Input: \@word = ($ints) and \$start = $start
+ Output: $output
+END
+}
+
+sub multiply_by_two ( $start, @ints ) {
+ while ( any { $start == $_ } @ints ) {
+ $start *= 2;
+ }
+ return $start;
+}