ENIGMAS - Enigma Machine

This challenge is to simulate the three rotor M3 Enigma Machine. For each test three lines of information will be provided; the first line will contain the rotor settings, the second the plugboard, and the third will be the text to encode/decode with the Enigma cipher.

Input

The first line will contain a single value T, for the number of tests to follow, where T <= 100.

For each test there will be three lines:

  • The first line will contain three entries indiciating: the Walzenlage, the Ringstellung, and the Grundstellung in the form '123 AAA BBB'. The Walzenlage only contains numbers 1-5. The Ringstellung and Grundstellung will be provided as triplets of letters in the range A-Z.
  • The second line will contain pairs of letters, from the range A-Z, indicating the settings for the Steckerbrett - there may be up to 13 pairs of letters.
  • The final line of the test will contain an unknown length message to encode/decode - the message will be in the standard quintuple form used at the time, separated with spaces, eg 'ABCDE FGHIJ KL'. The final group may be 1-5 letters in length. Again, only the range of letters A-Z will be used. The line will be terminated in a newline ('0x0A').

The Enigma machine used is the three rotor M3 version. This had five rotors, of which only three would have been installed at any time. The Umkehrwalze in use is the 'B' wiring.

Output

The output is to match the third line of the input, i.e. must be in the same quintuple grouping. One line of output per message is to be produced.

Example

Input:
2
123 JAN DER
2 123 JAN DER SP OJ RU LZ THISX ISXAN XEXAM PLEXI NPUT 543 SPO JPL SH OR TE NI YUQKD YVPSF HCQEI VHAPE NAQZQ I
SP OJ RU LZ
YUQKD YVPSF HCQEI VHAPE NAQZQ I
Output:
SJLKM SVZYM HXTUW VVWYY EDEB
THISX ISXAX DECOD XEDXM ESSAG E

Added by:Jander
Date:2012-10-04
Time limit:10s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:http://en.wikipedia.org/wiki/Enigma_machine

hide comments
2012-11-24 08:18:43 legrand
@Jander: I know my algo can't be shorten to 100B more. For the settings line ending problem see my submission 8116134 (SIGSEGV) where I don't use a gets() to clean the settings line ending.
EDIT:thanks for the help for the settings line ending.


Last edit: 2012-11-24 15:06:04
2012-11-23 09:52:02 Jander
@legrand - Line endings are all "\012" aka "\n". My code is based upon your 8070679 submission, and still uses exactly the same input methods.

You refer to code being obfuscated - my 577 byte code is *far* easier to read and simpler than your most recent submissions. Rather than looking at what your code is doing, consider what it is that it's trying to achieve - ie consider the algorithm, not the code, and then what can implement that algorithm for you.

I won't explain more here, but feel free to contact via the forum or via the #spoj IRC channel.
2012-11-23 09:01:11 legrand
My 789 codelength solution is still obsfurcated. To shorten more, I need to know more the input format: the end of the settings line is not \n nor \n\r and it requires extra-code. Also the end of steckerbrett line is unknown and there is room for shortening here.

Last edit: 2012-11-23 09:21:58
2012-11-23 07:28:33 Jander
@Aditya - That code still has whitespace in it :-) The actual code size is 577 bytes, I just haven't posted that one yet.

My "Challenger" user is used in the same way that Piotr KÄ…kol's is on the SHORTEN spoj site - A challenger to prove that submitters can do better :-) Now for you to see what improvements you can do in your code!
2012-11-16 21:33:50 Jander
@Aditya Pande - Not any more it isn't. Although, I think there is opportunity for significant improvement in all the C/C++ solutions. There is a lot of complexity that can still be removed/simplified.

Last edit: 2012-11-16 21:35:07
2012-11-15 09:05:51 Aditya Pande
@Jander :thanks
EDIT: mine is the best c solution
EDIT: not anymore :(

Edit
woah 606 B in C

Last edit: 2012-11-22 16:08:42
2012-11-14 12:43:35 Jander
@Aditya Pande - Check how your code behaves with multiple test cases in a single run. Your code isn't cleaning up properly between each case.
2012-11-12 07:45:39 Aditya Pande
@problemsetter plz give more test cases or tell me why my submission 8036036 fails and gets WA
because i have tested a lot of test cases myself

Last edit: 2012-11-16 03:52:32
2012-10-19 10:38:48 Jander
@legrand - as I'd already commented this challenge was aimed at SHORTEN from the outset. I figured that I'd open it to main Spoj to allow those that don't normally enter the SHORTEN arena a chance at something different.

This, to me, is one of the more "real" challenges in Spoj, so it's a nice change. It's already a simple machine so I can't see that there is much opportunity to speed it up. However, there are opportunities to making the code simpler. My 420 byte Perl solution is much simpler than my original reference code (written from first principles).
2012-10-19 10:00:57 legrand
I think putting the problem in shorten contest don't simplify the machine as in challenge to reduce the code we made obsfurcated one, and there will be perl or awk submissions.
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.