aboutsummaryrefslogtreecommitdiff
path: root/challenge-052
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-03-22 20:32:44 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-03-22 20:32:44 +0000
commit6d2d73ef54b27f1ddcde48607913692356753a8a (patch)
treee25c96ef74b696bc8efad91f6a0b59b7cca215ec /challenge-052
parentadc080d47c26396fcb85e1135390ca6d00dfea4f (diff)
downloadperlweeklychallenge-club-6d2d73ef54b27f1ddcde48607913692356753a8a.tar.gz
perlweeklychallenge-club-6d2d73ef54b27f1ddcde48607913692356753a8a.tar.bz2
perlweeklychallenge-club-6d2d73ef54b27f1ddcde48607913692356753a8a.zip
- Added solutions by Javier Luque.
Diffstat (limited to 'challenge-052')
-rw-r--r--challenge-052/javier-luque/blog.txt1
-rw-r--r--challenge-052/javier-luque/perl/ch-1.pl33
-rw-r--r--challenge-052/javier-luque/perl/ch-2.pl34
-rw-r--r--challenge-052/javier-luque/raku/ch-1.p624
-rw-r--r--challenge-052/javier-luque/raku/ch-2.p627
5 files changed, 119 insertions, 0 deletions
diff --git a/challenge-052/javier-luque/blog.txt b/challenge-052/javier-luque/blog.txt
new file mode 100644
index 0000000000..7df7e02b69
--- /dev/null
+++ b/challenge-052/javier-luque/blog.txt
@@ -0,0 +1 @@
+https://perlchallenges.wordpress.com/2020/03/16/perl-weekly-challenge-052/
diff --git a/challenge-052/javier-luque/perl/ch-1.pl b/challenge-052/javier-luque/perl/ch-1.pl
new file mode 100644
index 0000000000..03e686e09e
--- /dev/null
+++ b/challenge-052/javier-luque/perl/ch-1.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/perl
+# Test: ./ch-1.pl 100 999
+use strict;
+use warnings;
+use feature qw /say/;
+
+my $start = $ARGV[0];
+my $end = $ARGV[1];
+
+# Some input validation
+unless ($start && $end && $end > $start &&
+ $start >= 100 && $start < 1000 &&
+ $end >= 100 && $end < 1000) {
+
+ say "Usage: 100 999";
+ exit;
+}
+
+# Check each digit if it's stepping
+for my $i ($start .. $end) {
+ my $is_stepping = 1;
+ my $prev_digit;
+
+ my @digits = split ('', $i);
+ for my $digit (@digits) {
+ $is_stepping = 0 if
+ ( defined($prev_digit) &&
+ ( $prev_digit != $digit + 1 &&
+ $prev_digit != $digit - 1) );
+ $prev_digit = $digit;
+ }
+ say $i if ($is_stepping);
+}
diff --git a/challenge-052/javier-luque/perl/ch-2.pl b/challenge-052/javier-luque/perl/ch-2.pl
new file mode 100644
index 0000000000..b2f0f36103
--- /dev/null
+++ b/challenge-052/javier-luque/perl/ch-2.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/perl
+# Test: ./ch-2.pl --optimal
+use strict;
+use warnings;
+use Getopt::Long;
+use feature qw /say/;
+
+# Optimal flag
+my $optimal = 0;
+GetOptions ('optimal' => \$optimal);
+
+# Some initialization variables
+my @coins = (100, 50, 1, 10, 5, 20, 200, 2);
+my $players = 2;
+my $player_turn = 0;
+my @totals = map { 0 } 1 .. $players;
+
+# Play the game
+while (scalar(@coins) > 0) {
+ if ( ($optimal && $player_turn == 0) ||
+ $coins[0] > $coins[-1] ) {
+ $totals[$player_turn] += shift @coins;
+ } else {
+ $totals[$player_turn] += pop @coins;
+ }
+
+ # Next turn
+ $player_turn = ($player_turn + 1) % $players;
+}
+
+# Display the scores
+for my $i (1..$players) {
+ say "Player $i total: " . $totals[$i - 1] . 'p';
+}
diff --git a/challenge-052/javier-luque/raku/ch-1.p6 b/challenge-052/javier-luque/raku/ch-1.p6
new file mode 100644
index 0000000000..a87cc3b600
--- /dev/null
+++ b/challenge-052/javier-luque/raku/ch-1.p6
@@ -0,0 +1,24 @@
+#!/usr/bin/perl
+# Test: ./ch-6.p6
+
+multi MAIN { MAIN(100, 999) };
+multi MAIN(Int $start, Int $end) {
+ die "End smaller than start" if $end < $start;
+ .say for ($start .. $end).grep({is-stepping($_)});
+}
+
+sub is-stepping($num) {
+ my $is_stepping = True;
+ my $prev_digit;
+
+ for ($num.comb) -> $i {
+ $is_stepping = False
+ if ($prev_digit.defined &&
+ $prev_digit != $i + 1 &&
+ $prev_digit != $i - 1 );
+
+ $prev_digit = $i;
+ }
+
+ return $is_stepping;
+}
diff --git a/challenge-052/javier-luque/raku/ch-2.p6 b/challenge-052/javier-luque/raku/ch-2.p6
new file mode 100644
index 0000000000..98d9b9fa79
--- /dev/null
+++ b/challenge-052/javier-luque/raku/ch-2.p6
@@ -0,0 +1,27 @@
+# Test: perl6 ch-2.p6
+use v6.d;
+
+sub MAIN(Bool :$optimal = False) {
+ # Some initialization variables
+ my @coins = (100, 50, 1, 10, 5, 20, 200, 2);
+ my $players = 2;
+ my $player_turn = 0;
+ my @totals = map { 0 }, 1 .. $players;
+
+ # Play the game
+ while (@coins.elems > 0) {
+ if ( ($optimal && $player_turn == 0) ||
+ @coins[0] > @coins[*-1] ) {
+ @totals[$player_turn] += @coins.shift;
+ } else {
+ @totals[$player_turn] += @coins.pop;
+ }
+
+ # Next turn
+ $player_turn = ($player_turn + 1) % $players;
+ }
+
+ for (1..$players) -> $i {
+ say "Player $i total: " ~ @totals[$i - 1] ~ 'p';
+ }
+}