The Caesar cipher is a simple and famous cipher, where the letters of the alphabet are rotated by some secret amount. For example, if our secret rotation is 3
, we would replace a
with d
, b
with e
, w
with z
, x
with a
and so on.
Here is an example (rotation amount: 10):
Robo sc kx ohkwzvo
This cipher is very weak, because short common English words like "I", "a", "is", "an", "if", etc. are easy to detect. Your task is to crack a Caesar cipher, that is, recover the rotation amount from the ciphertext. As additional input, you are given a list (or set) of words, which the plaintext can contain. It is guaranteed that there is only one answer.
Examples
"Ifmmp Xpsme!", ["world", "banana", "hello"]
-> 1
"Nc cd, Kadcn?", ["cogito", "et", "ergo", "tu", "sum", "brute"]
-> 9
"boring", ["boring"]
-> 0
"bccb foo", ["abba", "gpp", "cddc"]
-> 25
" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~", ["zabcdefghijklmnopqrstuvwxy"]
-> 1
"bcsbdbebcsb", ["abracadabra", "za", "aq"]
-> 1
IO rules
The ciphertext can contain any printable ascii characters. The dictionary (list of words) contains strings made of lowercase letters (a-z). Words are separated by non-letters. Only letters are rotated (punctuation is ignored). You will output an integer in the range [0,25]
[1,26]
instead be acceptable (this is currently prohibited by your rules)? \$\endgroup\$"bcsbdbebcsb", ["abracadabra", "za", "aq"] -> 1
. If one looks for substrings of rotated words in the text, ignoring the fact that it is only one word long, then rotating2
will give two matching substrings, while rotating1
will only give one matching substring. \$\endgroup\$