From eb3dc2e59549db57fa49ce97688355010ac9a584 Mon Sep 17 00:00:00 2001 From: Torgny Lyon Date: Mon, 9 Sep 2024 18:30:59 +0200 Subject: Add solutions for week 286 --- challenge-286/torgny-lyon/blog.txt | 1 + challenge-286/torgny-lyon/perl/ch-1.pl | 8 ++++++++ challenge-286/torgny-lyon/perl/ch-2.pl | 22 ++++++++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 challenge-286/torgny-lyon/blog.txt create mode 100755 challenge-286/torgny-lyon/perl/ch-1.pl create mode 100755 challenge-286/torgny-lyon/perl/ch-2.pl (limited to 'challenge-286') diff --git a/challenge-286/torgny-lyon/blog.txt b/challenge-286/torgny-lyon/blog.txt new file mode 100644 index 0000000000..dc04de6771 --- /dev/null +++ b/challenge-286/torgny-lyon/blog.txt @@ -0,0 +1 @@ +https://www.abc.se/~torgny/pwc.html#286 diff --git a/challenge-286/torgny-lyon/perl/ch-1.pl b/challenge-286/torgny-lyon/perl/ch-1.pl new file mode 100755 index 0000000000..63bc588059 --- /dev/null +++ b/challenge-286/torgny-lyon/perl/ch-1.pl @@ -0,0 +1,8 @@ +#!/usr/bin/perl + +use v5.40; + +open my $f, '<', $0; +undef $/; +@_ = split /\s+/, <$f>; +say $_[ int rand @_ ]; diff --git a/challenge-286/torgny-lyon/perl/ch-2.pl b/challenge-286/torgny-lyon/perl/ch-2.pl new file mode 100755 index 0000000000..0044409085 --- /dev/null +++ b/challenge-286/torgny-lyon/perl/ch-2.pl @@ -0,0 +1,22 @@ +#!/usr/bin/perl + +use v5.40; + +use Test::More tests => 3; + +use List::Util qw(min max pairs); + +sub play_game { + return min(@_) if @_ == 2; + my $f = \&max; + return play_game( + map { + $f = $f == \&min ? \&max : \&min; + $f->($_->[0], $_->[1]); + } pairs @_ + ); +} + +is(play_game(2, 1, 4, 5, 6, 3, 0, 2), 1); +is(play_game(0, 5, 3, 2), 0); +is(play_game(9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8), 2); -- cgit