diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-02-17 10:37:09 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-02-17 10:37:09 +0000 |
| commit | a2a039b6a2dc669cbfffe56f172df6f7c01fd4c4 (patch) | |
| tree | d6617412639978a9488fed79e326269d3a5ada26 | |
| parent | 6553b8578f7bfc40bc8571718f6444d85a55e24f (diff) | |
| parent | e480debb260486025c8071f19ea4ccee61fabaa4 (diff) | |
| download | perlweeklychallenge-club-a2a039b6a2dc669cbfffe56f172df6f7c01fd4c4.tar.gz perlweeklychallenge-club-a2a039b6a2dc669cbfffe56f172df6f7c01fd4c4.tar.bz2 perlweeklychallenge-club-a2a039b6a2dc669cbfffe56f172df6f7c01fd4c4.zip | |
Merge pull request #1266 from davorg/master
Challenge #48
| -rw-r--r-- | challenge-048/dave-cross/perl/ch-01.pl | 50 | ||||
| -rw-r--r-- | challenge-048/dave-cross/perl/ch-02.pl | 25 |
2 files changed, 75 insertions, 0 deletions
diff --git a/challenge-048/dave-cross/perl/ch-01.pl b/challenge-048/dave-cross/perl/ch-01.pl new file mode 100644 index 0000000000..3330d5a952 --- /dev/null +++ b/challenge-048/dave-cross/perl/ch-01.pl @@ -0,0 +1,50 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use feature 'say'; + +my @people = (undef, (1) x 50); + +my $person_with_sword = 1; + +while () { + kill_someone($person_with_sword, \@people); + last unless more_than_one_person(@people); + $person_with_sword = hand_over_sword($person_with_sword, \@people); +} + +say "Last living person is #$person_with_sword"; + +sub more_than_one_person { + my $count = grep { $_ } @_; + return $count > 1; +} + +sub get_next_living_person { + my ($curr_person, $people) = @_; + + while (1) { + ++$curr_person; + $curr_person = 1 if $curr_person > $#$people; + + return $curr_person if $people->[$curr_person]; + } +} + +sub kill_someone { + my ($curr_person, $people) = @_; + + my $person_to_kill = get_next_living_person($curr_person, $people); + $people->[$person_to_kill] = 0; + + # say "$curr_person killed $person_to_kill"; + + return $person_to_kill; +} + +sub hand_over_sword { + my ($curr_person, $people) = @_; + + return get_next_living_person($curr_person, $people); +} diff --git a/challenge-048/dave-cross/perl/ch-02.pl b/challenge-048/dave-cross/perl/ch-02.pl new file mode 100644 index 0000000000..41d93def5c --- /dev/null +++ b/challenge-048/dave-cross/perl/ch-02.pl @@ -0,0 +1,25 @@ +#!/usr/bin/perl +# +# The original question appears to contain a typo as it suggests we use +# the illogical 'mmddyyyy' date format. My solution uses 'ddmmyyyy' instead. + +use strict; +use warnings; +use feature 'say'; + +use Time::Piece; +use Time::Seconds; + +my $fmt = '%d%m%Y'; + +my ($y, $m, $d) = (2000, 1, 1); +my $str_date = sprintf '%02d%02d%d', $d, $m, $y; +my $date = Time::Piece->strptime($str_date, $fmt); + +while ($date->year <= 2999) { + $str_date = $date->strftime($fmt); + if ($str_date eq reverse $str_date) { + say $date->ymd, " is a palindrome ($str_date)"; + } + $date += ONE_DAY; +} |
