diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-05-02 07:23:49 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-05-02 07:23:49 +0100 |
| commit | 226f45d48fb3dd9cb30034ebdf65e2eb1d281ded (patch) | |
| tree | 625e287fa5a69b1b2518385751e4564a30fb3c47 /challenge-162 | |
| parent | bec032f9ed6184666bcc342527fbdc0933d463a4 (diff) | |
| download | perlweeklychallenge-club-226f45d48fb3dd9cb30034ebdf65e2eb1d281ded.tar.gz perlweeklychallenge-club-226f45d48fb3dd9cb30034ebdf65e2eb1d281ded.tar.bz2 perlweeklychallenge-club-226f45d48fb3dd9cb30034ebdf65e2eb1d281ded.zip | |
- Added solutions by Kueppo Wesley.
Diffstat (limited to 'challenge-162')
| -rw-r--r-- | challenge-162/kueppo-wesley/perl/ch-1.pl | 19 | ||||
| -rw-r--r-- | challenge-162/kueppo-wesley/perl/ch-2.pl | 132 |
2 files changed, 151 insertions, 0 deletions
diff --git a/challenge-162/kueppo-wesley/perl/ch-1.pl b/challenge-162/kueppo-wesley/perl/ch-1.pl new file mode 100644 index 0000000000..9f30f40344 --- /dev/null +++ b/challenge-162/kueppo-wesley/perl/ch-1.pl @@ -0,0 +1,19 @@ +#!/usr/bin/env perl + +use strict; +use feature 'say'; +use v5.10; + +my $ps; + +$ps += $_ * &oot foreach (split /-|/, $ARGV[0]); +$ps %= 10; +$ps =~ s/[^0]/10 - $ps/e; + +say "$ARGV[0]-$ps"; + +sub oot { state $x = 3; $x = ($x == 3 ? 1 : 3) } + +## input : 978-0-306-40615 +## output : 978-0-306-40615-7 + diff --git a/challenge-162/kueppo-wesley/perl/ch-2.pl b/challenge-162/kueppo-wesley/perl/ch-2.pl new file mode 100644 index 0000000000..629e683684 --- /dev/null +++ b/challenge-162/kueppo-wesley/perl/ch-2.pl @@ -0,0 +1,132 @@ +#!/usr/bin/env perl + +use strict; +use feature 'say'; +use Test; + +BEGIN { plan test => 4 } + +sub generate_square { + my $key = $_[0]; + my @box; + + $key =~ s/ +//g; + $key =~ s/j/i/i; + do {;} while $key =~ s/(.)(.*?)\g{-2}/$1$2/; + $key .= join '', grep { $_ !~ /[$key]/ } split //, q(abcdefghiklmnopqrstuvwxyz); + push @box, $1 while $key =~ /(.{5})/g; + + return @box; +} + +sub generate_digrams { + my $text = $_[0]; + + $text =~ s/j/i/i; + $text =~ s/ +//g; + do {;} while $text =~ s/^((?:..)*?)(.)\g2/$1$2x$2/; + $text .= q(x) unless int (length($text) % 2) == 0; + + return $text; +} + +sub encrypt { + my $key = $_[0]; + my $text = $_[1]; + my @box = (); + my $cipher; + + @box = generate_square $key; + $text = generate_digrams $text; + + # cycle through digrams to generate ciphertext + while ($text =~ /(.)(.)/g) { + my $x = $1; + my $y = $2; + my ($xi, $xc) = (-1, -1); + my ($yi, $yc) = (-1, -1); + + foreach (my $i = 0; $i < @box; $i++) { + my $s = $box[$i]; + + if ($s =~ /$x(?|([^$y]).*$y(.)|($y)(.)).*$/) { $cipher .= "$1$2"; last; } + if ($s =~ /$y(?|([^$x]).*$x(.)|($x)(.)).*$/) { $cipher .= "$2$1"; last; } + elsif ($s =~ /^(.).*$x(?|([^$y]).*$y|($y))$/) { $cipher .= "$2$1"; last; } + elsif ($s =~ /^(.).*$y(?|([^$x]).*$x|($x))$/) { $cipher .= "$1$2"; last; } + else { + if ($s =~ /$x/) { $xi = index($s, $x); $xc = $i + 1; } + if ($s =~ /$y/) { $yi = index($s, $y); $yc = $i + 1; } + } + } + if ($xi > -1 && $yi > -1) { + my ($c1, $c2); + + if ($xi == $yi) { + $xc = 0 if $xc == @box; + $yc = 0 if $yc == @box; + ($c1, $c2) = ( substr($box[$xc], $xi, 1), substr($box[$yc], $yi, 1) ); + $cipher .= $c1 . $c2; + } + else { + ($c1, $c2) = ( substr($box[$xc - 1], $yi, 1), substr($box[$yc - 1], $xi, 1) ); + $cipher .= $c1 . $c2; + } + $xi = -1; $yi = -1; + } + } + + return $cipher; +} + +sub decrypt { + my $key = $_[0]; + my $cipher = $_[1]; + my $decrypt; + my @box; + + + @box = generate_square($key); + + while ($cipher =~ /(.)(.)/g) { + my $x = $1; + my $y = $2; + my ($xi, $xc) = (-1, -1); + my ($yi, $yc) = (-1, -1); + + foreach (my $i = 0; $i < @box; $i++) { + my $s = $box[$i]; + + if ($s =~ /(.)(?|($x)$y|$x.*(.)$y)/) { $decrypt .= "$1$2"; last; } + if ($s =~ /(.)(?|($y)$x|$y.*(.)$x)/) { $decrypt .= "$2$1"; last; } + elsif ($s =~ /^$y.*(.)(?|($x)|$x.*(.))$/) { $decrypt .= "$2$1"; last; } + elsif ($s =~ /^$x.*(.)(?|($y)|$y.*(.))$/) { $decrypt .= "$2$1"; last; } + else { + if ($s =~ /$x/) { $xi = index($s, $x); $xc = $i - 1; } + if ($s =~ /$y/) { $yi = index($s, $y); $yc = $i - 1; } + } + } + if ($xi > -1 && $yi > -1) { + my ($c1, $c2); + + if ($xi == $yi) { + $xc = 5 if $xc == @box; + $yc = 5 if $yc == @box; + ($c1, $c2) = ( substr($box[$xc], $xi, 1), substr($box[$yc], $yi, 1) ); + $decrypt .= $c1 . $c2; + } + else { + ($c1, $c2) = ( substr($box[$xc + 1], $yi, 1), substr($box[$yc + 1], $xi, 1) ); + $decrypt .= $c1 . $c2; + } + $xi = -1; $yi = -1; + } + } + + return $decrypt; +} + +ok encrypt("playfair example", "hide the gold in the tree stump"), "bmodzbxdnabekudmuixmmouvif"; +ok decrypt("playfair example", "bmodzbxdnabekudmuixmmouvif"), "hidethegoldinthetrexestump"; + +ok encrypt("perl and raku", "thewexeklychallengex"), "siderwrdulfipaarkcrw"; +ok decrypt("perl and raku", "siderwrdulfipaarkcrw"), "thewexeklychallengex"; |
