diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-07-06 19:41:40 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-07-06 19:41:40 +0100 |
| commit | af7315780cb497e270344e4d852cb0e036aadcbc (patch) | |
| tree | 0b2ba5ace98df1db16ec5915d967f5a98d6962db | |
| parent | 22208a51addb2edcbda154614c3200997b2564c0 (diff) | |
| parent | 7cd908d231c1130a6e1bb25f5732043b9e35fb24 (diff) | |
| download | perlweeklychallenge-club-af7315780cb497e270344e4d852cb0e036aadcbc.tar.gz perlweeklychallenge-club-af7315780cb497e270344e4d852cb0e036aadcbc.tar.bz2 perlweeklychallenge-club-af7315780cb497e270344e4d852cb0e036aadcbc.zip | |
Merge pull request #341 from jmaslak/joelle-15-2-1
Add solutions for 15.2.1
| -rwxr-xr-x | challenge-015/joelle-maslak/perl5/ch-2.pl | 45 | ||||
| -rwxr-xr-x | challenge-015/joelle-maslak/perl6/ch-2.p6 | 28 |
2 files changed, 73 insertions, 0 deletions
diff --git a/challenge-015/joelle-maslak/perl5/ch-2.pl b/challenge-015/joelle-maslak/perl5/ch-2.pl new file mode 100755 index 0000000000..076e7d7255 --- /dev/null +++ b/challenge-015/joelle-maslak/perl5/ch-2.pl @@ -0,0 +1,45 @@ +#!/usr/bin/env perl +use v5.26; +use strict; +use warnings; + +# Turn on method signatures +use feature 'signatures'; +no warnings 'experimental::signatures'; + +use autodie; + +die("Usage: $0 [--decrypt] <key>") unless @ARGV == 1 or @ARGV == 2; +die("Usage: $0 [--decrypt] <key>") unless @ARGV == 1 or $ARGV[0] eq '--decrypt'; +die("Usage: $0 [--decrypt] <key>") unless $ARGV[-1] =~ m/^ [a-z]+ $/isx; + +my $key = $ARGV[-1]; +my $decrypt = @ARGV == 2; + +my (@keys) = map { ord($_) - ord('a') } + split //, fc $key; + +# Do we decrypt? +@keys = map { 26 - $_ } @keys if $decrypt; + +my @input; +while ( my $line = <stdin> ) { push @input, split( //, $line ) } + +foreach my $c (@input) { + my $offset = $keys[0]; + + if ( $c =~ m/ [a-z] /x ) { + push @keys, shift @keys; # Circular buffer + my $new = ord($c) + $offset; + $new -= 26 if $new > ord('z'); + print chr($new); + } elsif ( $c =~ m/ [A-Z] /x ) { + push @keys, shift @keys; # Circular buffer + my $new = ord($c) + $offset; + $new -= 26 if $new > ord('Z'); + print chr($new); + } else { + print $c; + } +} + diff --git a/challenge-015/joelle-maslak/perl6/ch-2.p6 b/challenge-015/joelle-maslak/perl6/ch-2.p6 new file mode 100755 index 0000000000..087f79ed6d --- /dev/null +++ b/challenge-015/joelle-maslak/perl6/ch-2.p6 @@ -0,0 +1,28 @@ +#!/usr/bin/env perl6 +use v6; + +sub MAIN(Str:D $key where { $_ ~~ m:i/^ <[a..z]>+ $/ }, Bool :$decrypt? ) { + my @key-chars = $key.fc.comb; + + my @input = $*IN.slurp.comb; + + for @input -> $c { + my $offset = @key-chars[0].ord - 'a'.ord; + $offset = 26 - $offset if $decrypt; + + if $c ~~ m/ <[a..z]> / { + @key-chars.append: @key-chars.shift; # Circular buffer + my $new = $c.ord + $offset; + $new -= 26 if $new > 'z'.ord; + print $new.chr; + } elsif $c ~~ m/ <[A..Z]> / { + @key-chars.append: @key-chars.shift; # Circular buffer + my $new = $c.ord + $offset; + $new -= 26 if $new > 'Z'.ord; + print $new.chr; + } else { + print $c; + } + } +} + |
