diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-02-23 07:34:10 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-02-23 07:34:10 +0000 |
| commit | 4005d802a6cc3b03e300ea82d6bcd91887ea8c0c (patch) | |
| tree | ec9a91cbb5a11bd9165c8ea972b5d8af2061060d /challenge-048 | |
| parent | 48089a5a741b104dbbaa901cbc69058dfb746522 (diff) | |
| download | perlweeklychallenge-club-4005d802a6cc3b03e300ea82d6bcd91887ea8c0c.tar.gz perlweeklychallenge-club-4005d802a6cc3b03e300ea82d6bcd91887ea8c0c.tar.bz2 perlweeklychallenge-club-4005d802a6cc3b03e300ea82d6bcd91887ea8c0c.zip | |
- Added solutions by Kevin Colyer.
Diffstat (limited to 'challenge-048')
| -rw-r--r-- | challenge-048/kevin-colyer/raku/ch-1.p6 | 58 | ||||
| -rw-r--r-- | challenge-048/kevin-colyer/raku/ch-2.p6 | 22 |
2 files changed, 80 insertions, 0 deletions
diff --git a/challenge-048/kevin-colyer/raku/ch-1.p6 b/challenge-048/kevin-colyer/raku/ch-1.p6 new file mode 100644 index 0000000000..ed1dc05d32 --- /dev/null +++ b/challenge-048/kevin-colyer/raku/ch-1.p6 @@ -0,0 +1,58 @@ +#!perl6 +# Task 1 Challenge 048 Solution by +# +# Survivor +# +# There are 50 people standing in a circle in position 1 to 50. The person +# standing at position 1 has a sword. He kills the next person i.e. standing +# at position 2 and pass on the sword to the immediate next i.e. person standing +# at position 3. Now the person at position 3 does the same and it goes on +# until only one survives. +# +# Write a script to find out the survivor. +# + +# fill the circle with 50 alive people = 1's +my @circle = 1 xx 50; + +# helper function to loop around the circle looking for the next living person +sub nextAlive($i) { + my $j=$i; + loop { + # choose next person + $j++; + # loop back if reached end of array + $j=0 if $j>= @circle.elems; + # return index if that person is living... + return $j if @circle[$j]==1; + # prevent infinite loop check + die "No-one alive in circle" if $i==$j; + # and loop + } +} + +my $i=0; +my $j=-1; +say "1 has the sword..."; + +loop { + # choose victim + $j=nextAlive($i); + + # kill them + @circle[$j]=0; + say "{$i+1} killed {$j+1}"; + + # pass the sword on + my $k=nextAlive($j); + + # check exit the loop if we have just passed sword to ourselves - we are the only living one left + last if $i == $k; + + # pass the sword on + $i=$k; + say " and gave sword to {$i+1}"; + # and loop +} + +say "So the survivor is {$i+1}"; diff --git a/challenge-048/kevin-colyer/raku/ch-2.p6 b/challenge-048/kevin-colyer/raku/ch-2.p6 new file mode 100644 index 0000000000..b64834b2b1 --- /dev/null +++ b/challenge-048/kevin-colyer/raku/ch-2.p6 @@ -0,0 +1,22 @@ +#!perl6 +# Task 2 Challenge 048 Solution by +# +# Palindrome Dates +# +# Write a script to print all Palindrome Dates between 2000 and 2999. The +# format of date is mmddyyyy. For example, the first one was on October 2, +# 2001 as it is represented as 10022001. +# + +# Set up a custom string formatter to convert the date to the correct us format (see docs) +my $pwcformat= sub ($self) { sprintf "%02d%02d%04d", .month, .day, .year given $self;}; + +# Construct date object using custom formatter +my $date=Date.new(year => 2001,month=>10,day=>2, formatter => $pwcformat); +my $end=Date.new(year => 3000,month=>1,day=>1); + +while $date < $end { + my $d=$date.Str; + say $date.yyyy-mm-dd ~ " is palindrome: $d" if $d eq $d.flip; + $date.=succ; +} |
