aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-162/james-smith/perl/ch-2.pl28
1 files changed, 16 insertions, 12 deletions
diff --git a/challenge-162/james-smith/perl/ch-2.pl b/challenge-162/james-smith/perl/ch-2.pl
index c8e25e3ec3..d4e3edfb20 100644
--- a/challenge-162/james-smith/perl/ch-2.pl
+++ b/challenge-162/james-smith/perl/ch-2.pl
@@ -17,18 +17,22 @@ sub encrypt { return _crypt( 1,@_); }
sub decrypt { return _crypt(-1,@_); }
sub _crypt {
- my($off,$key,$p,$out,@r,%l) = (shift,shift,0,''); ## Initialise variables and get mapping...
- ($_ eq 'j' && ($_='i')), exists $l{$_} || ($l{$_}=[int $p/5,($p++)%5]) for grep { /[a-z]/ } split(//,$key),'a'..'i','j'..'z';
- $r[$l{$_}[0]][$l{$_}[1]]=$_ for keys %l;
-
- my @seq = grep {/[a-z]/} split //, shift =~ s{j}{j}gr; ## Prep sequence
-
- while(my($m,$n)=splice @seq,0,2) { ## Loop through letter pairs
- unshift(@seq,$n), $n='x' if $n && $n eq $m and $n ne 'x'; ## Deal with case when both letters the same
- $n ||= 'x'; ## Pad if required...
- $out.= $l{$m}[0] eq $l{$n}[0] ? $r[ $l{$m}[0] ][($l{$m}[1]+$off)%5] . $r[ $l{$n}[0] ][($l{$n}[1]+$off)%5]
- : $l{$m}[1] eq $l{$n}[1] ? $r[($l{$m}[0]+$off)%5][ $l{$m}[1] ] . $r[($l{$n}[0]+$off)%5][ $l{$n}[1] ]
- : $r[ $l{$m}[0] ][ $l{$n}[1] ] . $r[ $l{$n}[0] ][ $l{$m}[1] ]
+ my($o,$key,$p,$out,@r,%l) = (shift,shift,0,''); ## Initialise variables and get mapping...
+ ($_ eq 'j' && ($_='i')), exists $l{$_} || ($l{$_}=[int $p/5,($p++)%5]) ## %l maps letter to position
+ for grep { /[a-z]/ } split(//,$key),'a'..'i','k'..'z';
+ $r[$l{$_}[0]][$l{$_}[1]]=$_ for keys %l; ## @r maps position to letter
+
+ my @seq = grep {/[a-z]/} split //, shift =~ s{j}{i}gr; ## Prep sequence
+
+ while(my($m,$n)=splice @seq,0,2) { ## Loop through letter pairs
+ unshift(@seq,$n), $n='x' if $n && $n eq $m && $n ne 'x'; ## Deal with case when both letters the same
+ $n ||= 'x'; ## Pad if required...
+ $out.= $l{$m}[0] eq $l{$n}[0] ? $r[ $l{$m}[0] ][($l{$m}[1]+$o)%5].
+ $r[ $l{$n}[0] ][($l{$n}[1]+$o)%5]
+ : $l{$m}[1] eq $l{$n}[1] ? $r[($l{$m}[0]+$o)%5][ $l{$m}[1] ].
+ $r[($l{$n}[0]+$o)%5][ $l{$n}[1] ]
+ : $r[ $l{$m}[0] ][ $l{$n}[1] ].
+ $r[ $l{$n}[0] ][ $l{$m}[1] ]
;
}
$out;