#!/usr/bin/perl use strict; sub r{ my($r,$p,$c,$n)=@_; $r=~s/([a-z])/$n=uc($1),uc($1)/eg && $r=~s/(.*)($p)(.*)/$2$3$1/; sub{ if($_[0]=~/^-(.)$/){$p=('A'..'Z')[index($r,$1)]} else{$r=~s/(.{$_[1]})(.*)/$2$1/ if $#_>0;$p=substr($r,ord($_[0])-65,1)} ($p,eval $c) } } my $d=0; %_=map{chomp;(split/:/,$_,2)}; # These four lines read the setting data ;) $_{$_}=[split/ /,$_{$_}] for qw(position plug order); $_{'plug'}={map{@_=(split//);@_[0,1,1,0]}@{$_{'plug'}} }; $_{'switch'}=['$n eq $p','$d=($n eq $p),$d','$d?!($d=0):0','0']; @_=map{ &r($_{$_{'order'}->[$_]}, $_{'position'}->[$_], $_{'switch'}->[$_]) }(0..$#{$_{'order'}}); s/([a-z])/$_=uc($1); # Enigma takes only alpha chars. my($a,$i)=(1);$_=$_{'plug'}->{$_}|| $_; # go through stecker plugs. for $i(0..$#_){($_,$a)=&{$_[$i]}($_,$a)} # go through rotors. $_=substr($_{'reflect'},ord($_)-65,1); # go through reflector plate. for($i=$#_;$i>=0;){($_,$a)=&{$_[$i--]}("-$_")} # go back through rotors $_=$_{'plug'}->{$_}|| $_/eigx && print while(<>) # undergo steckering. __END__ rotor1:EKMFLGDqVZXNTOWYHUSPAIBRCJ rotor3:AJDKSIRUXBLHWTMCQGZNPYFVOe rotor2:BDFHJLCPRTXvZNYEIWGAKMUSQO rotor4:ESOVPZjAYQUIRHXlNFTGKDCMWB reflect:YRUHQSLDPXNGOKMIEBFZCWVJAT order:rotor1 rotor3 rotor2 position:D M Z A plug: AT MX CD GR