From d2b3c44d64c3fa9631fef9f7366ac9521259d2bb Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 9 Sep 2024 09:47:56 +0200 Subject: Add solutions to 286: Self Spammer & Order Game by E. Choroba --- challenge-286/e-choroba/perl/ch-1.pl | 11 +++++++++++ challenge-286/e-choroba/perl/ch-2.pl | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100755 challenge-286/e-choroba/perl/ch-1.pl create mode 100755 challenge-286/e-choroba/perl/ch-2.pl diff --git a/challenge-286/e-choroba/perl/ch-1.pl b/challenge-286/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..5d7490d54c --- /dev/null +++ b/challenge-286/e-choroba/perl/ch-1.pl @@ -0,0 +1,11 @@ +#!/usr/bin/perl +use warnings; +use strict; +use feature qw{ say }; + +my @words; +open my $self, '<', __FILE__ or die $!; +while (<$self>) { + push @words, split; +} +say $words[ rand @words ]; diff --git a/challenge-286/e-choroba/perl/ch-2.pl b/challenge-286/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..bf20ab9d36 --- /dev/null +++ b/challenge-286/e-choroba/perl/ch-2.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub order_game(@ints) { + return $ints[0] if 1 == @ints; + + my @mins; + while (my ($x, $y) = splice @ints, 0, 2) { + push @mins, ($x, $y)[($y <=> $x) == @mins % 2 * 2 - 1]; + } + return order_game(@mins) +} + +use Test::More tests => 3 + 2; + +is order_game(2, 1, 4, 5, 6, 3, 0, 2), 1, 'Example 1'; +is order_game(0, 5, 3, 2), 0, 'Example 2'; +is order_game(9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8), 2, 'Example 3'; + +is order_game(42), 42, 'Single number'; +is order_game(3, 7), 3, 'Single tuple'; -- cgit