From ea1ffe97b88ea6b4f595b2d3ee4cb76358e8cd79 Mon Sep 17 00:00:00 2001 From: Torgny Lyon Date: Wed, 17 Sep 2025 20:28:38 +0200 Subject: Add solutions for week 339 --- challenge-339/torgny-lyon/blog.txt | 1 + challenge-339/torgny-lyon/perl/ch-1.pl | 16 ++++++++++++++++ challenge-339/torgny-lyon/perl/ch-2.pl | 18 ++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 challenge-339/torgny-lyon/blog.txt create mode 100755 challenge-339/torgny-lyon/perl/ch-1.pl create mode 100755 challenge-339/torgny-lyon/perl/ch-2.pl diff --git a/challenge-339/torgny-lyon/blog.txt b/challenge-339/torgny-lyon/blog.txt new file mode 100644 index 0000000000..9828222192 --- /dev/null +++ b/challenge-339/torgny-lyon/blog.txt @@ -0,0 +1 @@ +https://www.abc.se/~torgny/pwc.html#339 diff --git a/challenge-339/torgny-lyon/perl/ch-1.pl b/challenge-339/torgny-lyon/perl/ch-1.pl new file mode 100755 index 0000000000..f63dc898fe --- /dev/null +++ b/challenge-339/torgny-lyon/perl/ch-1.pl @@ -0,0 +1,16 @@ +#!/usr/bin/perl + +use v5.42; + +use Test::More tests => 5; + +sub find_max_diff { + my @ints = sort { $b <=> $a } map { abs } @_; + $ints[0] * $ints[1] - $ints[$#ints] * $ints[$#ints - 1]; +} + +is(find_max_diff(5, 9, 3, 4, 6), 42); +is(find_max_diff(1, -2, 3, -4), 10); +is(find_max_diff(-3, -1, -2, -4), 10); +is(find_max_diff(10, 2, 0, 5, 1), 50); +is(find_max_diff(7, 8, 9, 10, 10), 44); diff --git a/challenge-339/torgny-lyon/perl/ch-2.pl b/challenge-339/torgny-lyon/perl/ch-2.pl new file mode 100755 index 0000000000..ad724c293d --- /dev/null +++ b/challenge-339/torgny-lyon/perl/ch-2.pl @@ -0,0 +1,18 @@ +#!/usr/bin/perl + +use v5.42; + +use Test::More tests => 5; + +use List::Util qw(max); + +sub find_peak_gain { + my $altitude; + max(0, map { $altitude += $_ } @_); +} + +is(find_peak_gain(-5, 1, 5, -9, 2), 1); +is(find_peak_gain(10, 10, 10, -25), 30); +is(find_peak_gain(3, -4, 2, 5, -6, 1), 6); +is(find_peak_gain(-1, -2, -3, -4), 0); +is(find_peak_gain(-10, 15, 5), 10); -- cgit