aboutsummaryrefslogtreecommitdiff
path: root/challenge-048
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-11-18 20:12:46 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-11-18 20:12:46 +0000
commit0d9e7c2c5551e2ab26da70fb1a07f40a06d8e5ba (patch)
tree3c22493a077342901e67221bc9f76c5386fc00a0 /challenge-048
parent3e386e736b3c53f8723ef6b731fb732395e502e4 (diff)
parent0a6fd9c3a63a5315ff28e0cd8a134ce2c081a7e7 (diff)
downloadperlweeklychallenge-club-0d9e7c2c5551e2ab26da70fb1a07f40a06d8e5ba.tar.gz
perlweeklychallenge-club-0d9e7c2c5551e2ab26da70fb1a07f40a06d8e5ba.tar.bz2
perlweeklychallenge-club-0d9e7c2c5551e2ab26da70fb1a07f40a06d8e5ba.zip
Merge branch 'new-branch' of git://github.com/duanepowell/perlweeklychallenge-club into duanepowell-new-branch
Diffstat (limited to 'challenge-048')
-rwxr-xr-xchallenge-048/duane-powell/perl5/ch-1.pl57
-rwxr-xr-xchallenge-048/duane-powell/perl5/ch-2.pl61
2 files changed, 118 insertions, 0 deletions
diff --git a/challenge-048/duane-powell/perl5/ch-1.pl b/challenge-048/duane-powell/perl5/ch-1.pl
new file mode 100755
index 0000000000..8ab2eb41d0
--- /dev/null
+++ b/challenge-048/duane-powell/perl5/ch-1.pl
@@ -0,0 +1,57 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use feature 'say';
+
+# Problem: https://perlweeklychallenge.org/blog/perl-weekly-challenge-048/ Task #1
+
+my $SWORDSMAN = shift || 50;
+
+# These constants are not needed but it could be fun to add more weapons and rules
+use constant {
+ NO_SWORD => 0,
+ SWORD => 1,
+};
+
+# Swordsman constructor
+sub new_swordsman {
+ my $name = shift;
+ my $armed = shift;
+ return {name => $name, armed => $armed, next => undef};
+}
+
+# Create n swordsman and arrange them in a cirle
+my $first_swordsman = new_swordsman(1, SWORD);
+my $swordsman = $first_swordsman;
+foreach my $s (2 .. $SWORDSMAN) {
+ my $next = new_swordsman($s, NO_SWORD);
+ # expand the circle and continue
+ $swordsman->{next} = $next;
+ $swordsman = $next;
+}
+$swordsman->{next} = $first_swordsman;
+
+# Execute man to your right and pass the sword until there is
+# only one man standing, i.e. the next swordsman is yourself
+$swordsman = $first_swordsman;
+until ($swordsman->{next} == $swordsman) {
+ my $condemned = $swordsman->{next}; # ID the condemned
+ my $next = $condemned->{next}; # ID who gets the SWORD next
+ $condemned = undef; # execute the condemned
+ $swordsman->{armed} = NO_SWORD; # pass the sword
+ $next->{armed} = SWORD;
+ # contract the circle and continue
+ $swordsman->{next} = $next;
+ $swordsman = $next;
+}
+
+say "$SWORDSMAN Swordsman arranged in a circle, the last man standing is Swordsman " . $swordsman->{name};
+
+__END__
+
+./ch-1.pl
+50 Swordsman arranged in a circle, the last man standing is Swordsman 37
+
+./ch-1.pl 3
+3 Swordsman arranged in a circle, the last man standing is Swordsman 3
+
diff --git a/challenge-048/duane-powell/perl5/ch-2.pl b/challenge-048/duane-powell/perl5/ch-2.pl
new file mode 100755
index 0000000000..8a4d2a7033
--- /dev/null
+++ b/challenge-048/duane-powell/perl5/ch-2.pl
@@ -0,0 +1,61 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use feature 'say';
+use DateTime;
+use DateTime::Duration;
+
+# Problem: https://perlweeklychallenge.org/blog/perl-weekly-challenge-048/ Task #2
+
+my $year_end = shift || 3000;
+
+my $dt = DateTime->new('year' => 2000, 'month' => 1, 'day' => 1);
+my $day = DateTime::Duration->new('days' => 1);
+
+until ($dt->year == $year_end) {
+ my @pali = split('',$dt->mdy);
+ # @pali is a 10 element array MM-DD-YYYY
+ say $dt->mdy() if ($pali[0] == $pali[9] and $pali[1] == $pali[8] and $pali[3] == $pali[7] and $pali[4] == $pali[6]);
+ $dt->add( $day );
+}
+
+__END__
+
+./ch-2.pl
+10-02-2001
+01-02-2010
+11-02-2011
+02-02-2020
+12-02-2021
+03-02-2030
+04-02-2040
+05-02-2050
+06-02-2060
+07-02-2070
+08-02-2080
+09-02-2090
+10-12-2101
+01-12-2110
+11-12-2111
+02-12-2120
+12-12-2121
+03-12-2130
+04-12-2140
+05-12-2150
+06-12-2160
+07-12-2170
+08-12-2180
+09-12-2190
+10-22-2201
+01-22-2210
+11-22-2211
+02-22-2220
+12-22-2221
+03-22-2230
+04-22-2240
+05-22-2250
+06-22-2260
+07-22-2270
+08-22-2280
+09-22-2290
+