Introduction:
I have loads of different ciphers stored in a document I once compiled as a kid, I picked a few of the ones I thought were best suitable for challenges (not too trivial, and not too hard) and transformed them into challenges. Most of them are still in the sandbox, and I'm not sure yet whether I'll post all of them, or only a few. Here is the second one (the Computer Cipher was the first one I posted).
For the Trifid Cipher (without using a keyword) the alphabet (and an additional wildcard) is divided into three 3 by 3 tables:
table 1: table 2: table 3:
|1 2 3 |1 2 3 |1 2 3
-+----- -+----- -+-----
1|a b c 1|j k l 1|s t u
2|d e f 2|m n o 2|v w x
3|g h i 3|p q r 3|y z
A text we want to encipher is first character by character encoded into table-row-column numbers. For example, the text this is a trifid cipher
becomes:
t h i s i s a t r i f i d c i p h e r
table: 3 1 1 3 3 1 3 3 1 3 3 2 1 1 1 1 3 1 1 2 1 1 2
row: 1 3 3 1 3 3 1 3 1 3 1 3 3 2 3 2 3 1 3 3 3 2 3
column: 2 2 3 1 3 3 1 3 1 3 2 3 3 3 3 1 3 3 3 1 2 2 3
We then put everything after one another row by row in the table above in groups of three:
311 331 331 332 111 131 121 121 331 331 313 133 232 313 332 322 313 313 132 333 313 331 223
And those are transformed back to characters using the same tables:
s y y z a g d d y y u i q u z w u u h u y o
One thing to note, the input-length should be coprime to 3. So if the length is a multiple of 3, we append one or two trailing spaces to make the input-length not a multiple 3 anymore.
Challenge:
Given a string sentence_to_encipher
, encipher it as described above.
You only have to encipher given the sentence_to_encipher
, so no need to create a deciphering program/function as well. I might make a part 2 challenge for the deciphering in the future however (although I have the feeling it's to trivial/similar to the enciphering process).
Challenge rules:
- You can assume the
sentence_to_encipher
will only contain letters and spaces. - You can use either full lowercase or full uppercase (please state which one you've used in your answer).
- You can choose to append either one or two trailing spaces when the input-length is 3 to make it not a multiple of 3 anymore.
- I/O is flexible. Both input and output can be a string, list/array/stream of characters, etc.
General rules:
- This is code-golf, so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language. - Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
- Default Loopholes are forbidden.
- If possible, please add a link with a test for your code (i.e. TIO).
- Also, adding an explanation for your answer is highly recommended.
Test cases:
Input: "this is a trifid cipher"
Output: "syyzagddyyuiquzwuuh uyo"
Input: "test"
Output: "utbk"
Input: "output"
Possible outputs: "rrvgivx" (one space) or "rrzcc lr" (two spaces)
Input: "trifidcipher"
Possible output: "vabbuxlzz utr" (one space) or "vabbyzv rx ie " (two spaces)