Introduction
The Enigma was one of the first electro-mechanical rotor cipher machines used in World War II. That means that after a single letter is coded, it would change the key for the next letter. This was considered unbreakable by the Germans, due to the enormous key space. Even brute-forcing was almost impossible. However, there was a design error in the Enigma. Encrypting a letter would never result into itself. That means that the letter A
can encrypt to every letter except the letter A
.
Let's take an example of a coded message:
BHGEFXWFTIUPITHHLPETTTCLOEWOELMRXXPAKAXMAMTXXUDLTWTNHKELEPPLHPRQ
A typical German word was WETTERBERICHT
, or weather report in English. With the principe above, we can determine at which locations the word could possibly be:
BHGEFXWFTIUPITHHLPETTTCLOEWOELMRXXPAKAXMAMTXXUDLTWTNHKELEPPLHPRQ
WETTERBERICHT
^
This is not possible, because the I
can't be encrypted to itself, so we move on 1 place:
BHGEFXWFTIUPITHHLPETTTCLOEWOELMRXXPAKAXMAMTXXUDLTWTNHKELEPPLHPRQ
WETTERBERICHT
^
This is also not possible, so we move another place again:
BHGEFXWFTIUPITHHLPETTTCLOEWOELMRXXPAKAXMAMTXXUDLTWTNHKELEPPLHPRQ
WETTERBERICHT
^
This again is not possible. In fact, the first possible occurence of WETTERBERICHT
is:
BHGEFXWFTIUPITHHLPETTTCLOEWOELMRXXPAKAXMAMTXXUDLTWTNHKELEPPLHPRQ
WETTERBERICHT
0123456789012345678901234567890123456789012345678901234567890123
^
13
So, we return the 0-indexed position of the first possible occurence, which is 13.
The Task
- Given a coded message and a word, find the index of the first possible occurence.
- Assume that only basic uppercase alphabetic characters will be used (
ABCDEFGHIJKLMNOPQRSTUVWXYZ
). - If no occurence is found, you can output any negative integer, character, or nothing (e.g.
-1
,X
). - Input may be accepted as argument, on seperate newlines, lists or anything else.
- This is code-golf, so the submission with the least amount of bytes wins!
Test cases
Input: BHGEFXWFTIUPITHHLPETTTCLOEWOELM, WETTERBERICHT
Output: 13
Input: ABCDEFGHIJKL, HELLO
Output: 0
Input: EEEEEEEEEEEE, HELLO
Output: -1
Input: XEEFSLBSELDJMADNADKDPSSPRNEBWIENPF, DEUTSCHLAND
Output: 11
Input: HKKH, JJJJJ
Output: -1
E
would never result into anE
. That is what this whole challenge is about. \$\endgroup\$