aboutsummaryrefslogtreecommitdiff
path: root/challenge-192
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2022-11-22 19:49:50 +0100
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2022-11-25 22:00:31 +0100
commita89a23b15fbd64b2ca9852833881a5f8aee46d85 (patch)
tree0f4a4d6ef934850dc597f6cfcb0f93590674a6e0 /challenge-192
parente1c415fa3ff565793523c0645d27154ad5022e80 (diff)
downloadperlweeklychallenge-club-a89a23b15fbd64b2ca9852833881a5f8aee46d85.tar.gz
perlweeklychallenge-club-a89a23b15fbd64b2ca9852833881a5f8aee46d85.tar.bz2
perlweeklychallenge-club-a89a23b15fbd64b2ca9852833881a5f8aee46d85.zip
Solution to task 2
Diffstat (limited to 'challenge-192')
-rwxr-xr-xchallenge-192/jo-37/perl/ch-2.pl67
1 files changed, 67 insertions, 0 deletions
diff --git a/challenge-192/jo-37/perl/ch-2.pl b/challenge-192/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..7739cca720
--- /dev/null
+++ b/challenge-192/jo-37/perl/ch-2.pl
@@ -0,0 +1,67 @@
+#!/usr/bin/perl -s
+
+use v5.16;
+use Test2::V0;
+use List::Util qw(reduce reductions);
+
+our ($tests, $examples, $verbose);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [N...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+N...
+ non-negative integers
+
+EOS
+
+
+### Input and Output
+
+say num_moves(@ARGV);
+
+
+### Implementation
+
+# See the blog
+# https://github.com/jo-37/perlweeklychallenge-club/blob/master/challenge-192/jo-37/blog.pdf
+# for a derivation of the formula behind this implementation.
+
+sub num_moves {
+ my @cum = reductions {$a + $b} @_;
+ return -1 if $cum[-1] % @_;
+ my ($eq, $step) = (0, $cum[-1] / @_);
+
+ reduce {$a + abs($b - ($eq += $step))} 0, @cum;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is num_moves(1, 0, 5), 4, 'Example 1';
+ is num_moves(0, 2, 0), -1, 'Example 2';
+ is num_moves(0, 3, 0), 2, 'Example 3';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is num_moves(1, 2, 3, 4, 5, 6, 7), 28, 'counted by hand';
+ is num_moves(5, 5, 5, 5, 5), 0, 'equilibrium';
+ is num_moves(5, 0, 0, 0, 0), 10, '4 + 3 + 2 + 1';
+ }
+
+ done_testing;
+ exit;
+}