diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2020-02-22 10:11:20 +0100 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2020-02-22 10:11:20 +0100 |
| commit | d26ef475d9d8b1cfc38fb0cec81aa6af5aba8ec6 (patch) | |
| tree | ab9c5b888d8e6e107f301264930fe7bac16de626 | |
| parent | 5538168ffe1104ff3b90a97f0ba6b66eb2af8cd8 (diff) | |
| download | perlweeklychallenge-club-d26ef475d9d8b1cfc38fb0cec81aa6af5aba8ec6.tar.gz perlweeklychallenge-club-d26ef475d9d8b1cfc38fb0cec81aa6af5aba8ec6.tar.bz2 perlweeklychallenge-club-d26ef475d9d8b1cfc38fb0cec81aa6af5aba8ec6.zip | |
Challenge 1 Perl
| -rw-r--r-- | challenge-048/lubos-kolouch/perl/ch-1.pl | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/challenge-048/lubos-kolouch/perl/ch-1.pl b/challenge-048/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..f8f6e39eac --- /dev/null +++ b/challenge-048/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,67 @@ +#!/usr/bin/perl +#=============================================================================== +# +# FILE: ch-1.pl +# +# USAGE: ./ch-1.pl +# +# DESCRIPTION: https://perlweeklychallenge.org/blog/perl-weekly-challenge-048/ +# +# 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. +# +# OPTIONS: --- +# REQUIREMENTS: --- +# BUGS: --- +# NOTES: --- +# AUTHOR: Lubos Kolouch +# ORGANIZATION: +# VERSION: 1.0 +# CREATED: 02/21/2020 09:15:59 PM +# REVISION: --- +#=============================================================================== + +use strict; +use warnings; +use feature qw/say/; + +sub get_last_man_standing { + my $count = shift; + + my %people; + + for ( 1 .. $count ) { + $people{$_} = 1; + } + + my $last = 0; + my $switch = 0; + + while (%people) { + for my $key ( sort { $a <=> $b } keys %people ) { + + delete $people{$key} if $switch; + + $last = $key; + $switch = $switch == 0 ? 1 : 0; + } + } + + return $last; +} + +my $people_count = $ARGV[0]; + +say get_last_man_standing($people_count); + +# TESTS + +use Test::More tests => 3; + +is get_last_man_standing(50),37; +is get_last_man_standing(2),1; +is get_last_man_standing(3),3; + |
