aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-11-23 03:58:33 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-11-23 03:58:33 +0000
commit96a22f4411a2b8fe9f7879c6289d7a2f265fd585 (patch)
tree452beb7f057fce62f23c4ef4de7e49ccdb51b267
parent77c84beed78906d0ea68ec0762f127dbf8f61ae5 (diff)
downloadperlweeklychallenge-club-96a22f4411a2b8fe9f7879c6289d7a2f265fd585.tar.gz
perlweeklychallenge-club-96a22f4411a2b8fe9f7879c6289d7a2f265fd585.tar.bz2
perlweeklychallenge-club-96a22f4411a2b8fe9f7879c6289d7a2f265fd585.zip
- Tidied up solutions by Duane Powell.
-rwxr-xr-xchallenge-048/duane-powell/perl/ch-1.pl14
-rwxr-xr-xchallenge-048/duane-powell/perl5/ch-1.pl57
-rwxr-xr-xchallenge-048/duane-powell/perl5/ch-2.pl61
3 files changed, 3 insertions, 129 deletions
diff --git a/challenge-048/duane-powell/perl/ch-1.pl b/challenge-048/duane-powell/perl/ch-1.pl
index 1c8213b45e..8ab2eb41d0 100755
--- a/challenge-048/duane-powell/perl/ch-1.pl
+++ b/challenge-048/duane-powell/perl/ch-1.pl
@@ -9,7 +9,6 @@ my $SWORDSMAN = shift || 50;
# These constants are not needed but it could be fun to add more weapons and rules
use constant {
- KILL => undef,
NO_SWORD => 0,
SWORD => 1,
};
@@ -21,7 +20,7 @@ sub new_swordsman {
return {name => $name, armed => $armed, next => undef};
}
-# Create a set of swordsman and arrange them in a cirle
+# 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) {
@@ -30,7 +29,6 @@ foreach my $s (2 .. $SWORDSMAN) {
$swordsman->{next} = $next;
$swordsman = $next;
}
-# Complete the cirle by linking last swordsman to the first
$swordsman->{next} = $first_swordsman;
# Execute man to your right and pass the sword until there is
@@ -39,8 +37,8 @@ $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 = KILL; # execute the condemned
- $swordsman->{armed} = NO_SWORD; # pass the sword, not needed but fun to simulate
+ $condemned = undef; # execute the condemned
+ $swordsman->{armed} = NO_SWORD; # pass the sword
$next->{armed} = SWORD;
# contract the circle and continue
$swordsman->{next} = $next;
@@ -57,9 +55,3 @@ __END__
./ch-1.pl 3
3 Swordsman arranged in a circle, the last man standing is Swordsman 3
-./ch-1.pl 256
-256 Swordsman arranged in a circle, the last man standing is Swordsman 1
-
-if we step thru different circle sizes we note that Swordsman 1 lives if the cirle
-size is a power of 2: 1,2,4,8,16 ... 256
-
diff --git a/challenge-048/duane-powell/perl5/ch-1.pl b/challenge-048/duane-powell/perl5/ch-1.pl
deleted file mode 100755
index 8ab2eb41d0..0000000000
--- a/challenge-048/duane-powell/perl5/ch-1.pl
+++ /dev/null
@@ -1,57 +0,0 @@
-#!/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
deleted file mode 100755
index 8a4d2a7033..0000000000
--- a/challenge-048/duane-powell/perl5/ch-2.pl
+++ /dev/null
@@ -1,61 +0,0 @@
-#!/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
-