aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Jacoby <jacoby.david@gmail.com>2024-09-11 22:54:43 -0400
committerDave Jacoby <jacoby.david@gmail.com>2024-09-11 22:54:43 -0400
commitff8719c86653d5ad3121955e9494a0010527c2b9 (patch)
tree8bb6c41a67b998943d3376abd1c31e59640a6a9b
parentc2f230caf21a77fdbe6b71da5b305dc91a91fff7 (diff)
downloadperlweeklychallenge-club-ff8719c86653d5ad3121955e9494a0010527c2b9.tar.gz
perlweeklychallenge-club-ff8719c86653d5ad3121955e9494a0010527c2b9.tar.bz2
perlweeklychallenge-club-ff8719c86653d5ad3121955e9494a0010527c2b9.zip
DAJ 286
-rw-r--r--challenge-286/dave-jacoby/perl/ch-1.pl14
-rw-r--r--challenge-286/dave-jacoby/perl/ch-2.pl49
2 files changed, 63 insertions, 0 deletions
diff --git a/challenge-286/dave-jacoby/perl/ch-1.pl b/challenge-286/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..8cc7bda72d
--- /dev/null
+++ b/challenge-286/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,14 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ postderef say signatures state };
+
+my $name = $0;
+my $file = '';
+if ( open my $fh, '<', $name ) {
+ $file = join '', <$fh>;
+ my @file = split /\s+/, $file;
+ say $file[ rand scalar @file ] ;
+}
+else { exit }
diff --git a/challenge-286/dave-jacoby/perl/ch-2.pl b/challenge-286/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..dda5e3b848
--- /dev/null
+++ b/challenge-286/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,49 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ say state postderef signatures };
+
+use List::Util qw{ min max };
+
+my @examples = (
+
+ [ 2, 1, 4, 5, 6, 3, 0, 2 ],
+ [ 0, 5, 3, 2 ],
+ [ 9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8 ],
+
+);
+
+for my $example (@examples) {
+ my $output = order_game($example);
+ my $input = join ', ', $example->@*;
+ say <<"END";
+ Input: \$ints = ($input)
+ Output: $output
+END
+}
+
+sub order_game ($ref) {
+ my @ints = $ref->@*;
+ my @output;
+
+ while ( scalar @ints > 1 ) {
+ if ( scalar @ints ) {
+ my @cmp;
+ push @cmp, shift @ints;
+ push @cmp, shift @ints;
+ push @output, min @cmp;
+ }
+ if ( scalar @ints ) {
+ my @cmp;
+ push @cmp, shift @ints;
+ push @cmp, shift @ints;
+ push @output, max @cmp;
+ }
+ if ( scalar @ints == 0 && scalar @output > 1 ) {
+ @ints = @output;
+ @output = ();
+ }
+ }
+ return shift @output;
+}