Description
A Caesar Shift is a very simple monoalphabetic cipher where each letter gets replaced by the one after it in the alphabet. Example:
Hello world! -> IFMMP XPSME!
(IBSLR, EGUFV!
is the output for the actual challenge, this was an example of shifting by 1.)
As you can see, spacing and punctuation remain unattuned. However, to prevent guessing the message, all letters are capitalized. By shifting the letters back, the message was deciphered, convenient, but also really easy to decipher by other persons who are supposed not to know what the message means.
So, we'll be helping Caesar a bit by using an advanced form of his cipher: the Self-shifting Caesar Shift!
Challenge
Your task is to write a program or function, that, given a string to encipher, outputs the encrypted string corresponding to the input. The advanced Caesar Shift works like this:
1. Compute letter differences of all adjacent letters:
1.1. Letter difference is computed like this:
Position of 2nd letter in the alphabet
-Position of 1st letter in the alphabet
=======================================
Letter difference
1.2. Example input: Hello
H - e|e - l|l - l|l - o
7 - 5|5 - 12|12 - 12|12 - 15 Letter differences: 3; -7; 0; -3
=3| =-7| =0| =-3
2. Assign the letters continously a letter difference from the list,
starting at the second letter and inverting the differences:
2.1. 2nd letter: first difference, 3rd letter: second difference, etc.
2.2. The first letter is assigned a 1.
2.3. Example input: Hello with differences 3; -7; 0; -3
Letter || Value
=======||======
H || 1
E || -3
L || 7
L || 0
O || 3
3. Shift the letters by the value x they have been assigned:
3.1. In case of a positive x, the letter is shifted x letters to the right.
3.2. In case of a negative x, the letter is shifted |x| letters to the left.
3.3. In case of x = 0, the letter is not shifted.
3.4. If the shift would surpass the limits of the alphabet, it gets wrapped around
Example: Y + Shift of 2 --> A
3.5. Example input: See the table under 2.3.
|| || Shifted
Letter || Value || Letter
=======||=======||=========
H || 1 || I
E || -3 || B Program output:
L || 7 || S IBSLR
L || 0 || L
O || 3 || R
Spaces and other special symbols, such as punctuation are skipped in this process. It is guaranteed that your program will be given a string containing only printable ASCII characters. The output of your function/program must only be in upper case.
This is code-golf, so standard loopholes apply, and may the shortest answer in bytes win!
E
-3
? \$\endgroup\$ZEN
, for example.Z
shifted by 1 is...A
? (as a side note, the 05AB1E answer turnsZ
intoA
) \$\endgroup\$RELIEF
andRELIES
to both encipher to the same resultSRSFAG
? \$\endgroup\$