Let's Encrypt It!

12

2

Challenge

The challenge is to encrypt a given string, using the rules as specified below. The string will only contain lowercase alphabets, digits, and/or blank spaces.

Equivalent of a Character

Now, firstly you would need to know how to find the "equivalent" of each character.

If the character is a consonant, this is the way of finding it's equivalent:

1) List all the consonants in alphabetical order
    b c d f g h j k l m n p q r s t v w x y z
2) Get the position of the consonant you are finding the equivalent of.
3) The equivalent is the consonant at that position when starting from the end.

eg: 'h' and 't' are equivalents of each other because 'h', 't' are in the 6th position from start and end respectively.

The same procedure is followed to find the equivalent of vowels/digits. You list all the vowels or the digits (starting from 0) in order and find the equivalent.

Given below is the list of the equivalents of all the characters:

b <-> z
c <-> y
d <-> x
f <-> w
g <-> v
h <-> t
j <-> s
k <-> r
l <-> q
m <-> p
n <-> n

a <-> u
e <-> o
i <-> i

0 <-> 9
1 <-> 8
2 <-> 7
3 <-> 6
4 <-> 5

Rules of Encrypting

1) You start moving from the left and go towards the right.

2) If the character is a consonant/digit, then its equivalent is taken and if it is a blank space, then a blank space is taken.

3) If the character is a vowel, you take it's equivalent and start to move in the opposite direction. For example, if you are moving right and encounter a vowel, encrypt that character then skip to the rightmost unencrypted character and begin encrypting in the left direction, and vice versa.

4) You shouldn't consider a character in the same position twice. The steps should be followed until all the characters in the input are covered.

5) The total number of characters in the input(including blank spaces) should be equal to the total number of characters in the output.

Please note that the encrypted characters appear in the output in the order in which they were encrypted.

Now let me encrypt a string for you.

String = "tre d1go3t is"
Moving left to right
"t" -> "h"
"r" -> "k"
"e" -> "o"
Vowel encountered. Now moving right to left.
"s" -> "j"
"i" -> "i"
Vowel encountered. Now moving left to right.
" " -> " "
"d" -> "x"
"1" -> "8"
"g" -> "v"
"o" -> "e"
Vowel encountered. Now moving right to left.
" " -> " "
"t" -> "h"
"3" -> "6"

Output -> "hkoji x8ve h6"

Examples

"flyspy" -> "wqcjmc"
"hero" -> "toek"
"heroic" -> "toyike"
"ae" -> "uo"
"abe" -> "uoz"
"the space" -> "htoo jmuy"
"a d1g13t" -> "uh68v8x "
"we xi12" -> "fo78i d"
"this is a code" -> "htioj ixej uy "

You may also choose to use uppercase alphabets instead of lowercase.

Scoring

This is , so the shortest code wins!

Manish Kundu

Posted 2018-02-08T00:59:36.533

Reputation: 1 947

1Step 3 is a bit unclear with regards to switching directions. I think you should say something like "If you are moving right and encounter a vowel, encrypt that character then skip to the rightmost unencrypted character and begin encrypting in the left direction." (If that is what you mean). I think you should also specify explicitly that the encrypted characters appear in the output in the order in which they were encrypted. – dylnan – 2018-02-08T01:11:24.363

@dylnan Added that. – Manish Kundu – 2018-02-08T01:14:54.730

Just out-of-curiosity - Can you describe decryption procedure ? Because encryption function is not it's own inverse (like in ROT13 algo). So if we pass an encrypted data to the same cipher procedure - we will not get the original text. Thanks – Agnius Vasiliauskas – 2018-02-08T09:30:34.460

Decryption can be done in the opposite way. You will get the original text on decrypting – Manish Kundu – 2018-02-08T09:57:05.213

1@AgniusVasiliauskas: One way of doing it would be: Apply the same character transformations. Keep 2 decrypt strings. Loop over the string left-to-right. Alternate between appending chars to the first string and prepending to the second every time you handle a vowel. Merge the strings at the end. – Emigna – 2018-02-08T10:00:03.933

3There will soon be a decryption challenge for the same, in which I will try to explain the process – Manish Kundu – 2018-02-08T10:38:19.807

For the curious, in Indian English alphabet means letter. – TRiG – 2018-02-08T11:22:39.450

Who downvoted, And why? – Manish Kundu – 2018-02-08T13:42:02.587

Answers

4

JavaScript (Node.js), 173 ... 166 156 ... 124 123 bytes

-28 byte Thanks Arnauld

f=([q,...s])=>q?(c="aeioubcdfghjklmpqrstvwxyz",t=c.search(q),q=="0"|+q?9-q:~t?c[(t<5?4:29)-t]:q)+f(~t&&t<5?s.reverse():s):s

Try it online!

In the first iteration the String will be changed to Array, and subsequent iterations will continue to use Array. Voilà!

Original Approach (166 bytes):

f=(s,i=0,r=s.length,d=1,c="bcdfghjklmnpqrstvwxyz",v="aeiou")=>(d^=!!(t=~v.search(q=s[d?i:r])),q<"0"|q>"9"?c[20-c.search(q)]||v[5+t]||q:9-q)+(i<r-1?f(s,i+d,r-!d,d):"")

Shieru Asakoto

Posted 2018-02-08T00:59:36.533

Reputation: 4 445

& didn't work for some numbers but && worked. Thanks. – Shieru Asakoto – 2018-02-08T07:56:40.040

Oh yes, I didn't find a method to optimize that and you did it! Thanks! – Shieru Asakoto – 2018-02-08T08:07:54.497

3124 bytes by using the same string for all letters and applying some more golfing. – Arnauld – 2018-02-08T09:41:39.687

Wow brilliant! I didn't think of combining the strings AT ALL – Shieru Asakoto – 2018-02-08T10:01:10.930

q=="0"|+q is actually 1 byte shorter than q>" "&&1/q. – Arnauld – 2018-02-08T11:31:55.763

3

05AB1E, 22 bytes

vćžN‡žM‡žh‡D?žMsåiR

Try it online! or as a Test suite

Explanation

v                        # for each char in input
 ć                       # extract the head of the current string (initially input)
  žN‡                   # transform consonants
      žM‡               # transofrm vowels
          žh‡           # transform numbers
              D?         # print a copy of the current char
                žMsåi    # if the current char is a vowel
                     R   # reverse the rest of the string

Emigna

Posted 2018-02-08T00:59:36.533

Reputation: 50 798

žhžMžN)UvćXJXíJ‡D?žMsåiR was what I was thinking for improvement, but can't cut XJXiJ down enough. – Magic Octopus Urn – 2018-02-08T14:34:34.360

@MagicOctopusUrn: I had a similar idea with DJsíJ which wasn't very effective either. – Emigna – 2018-02-08T15:02:01.083

1

C, 196 bytes

#define C(k,m)for(i=m;i--;)k[i]-c||putchar(k[m+~i],r^=m==5);
r;p(c,i){C("bcdfghjklmnpqrstvwxyz",21)C("0123456789",10)C("aeiou",5)C(" ",1)}f(S){char*s=S,*t=s+strlen(s);for(r=1;s<t;)p(r?*s++:*--t);}

Try it online!

Steadybox

Posted 2018-02-08T00:59:36.533

Reputation: 15 798

1

Clean, 221 206 198 190 186 178 bytes

import StdEnv
r=reverse
l=['bcdfghjklm01234aeou56789pqrstvwxyz']
$s#(a,b)=span(\e=all((<>)e)['aeiou'])s
|s>[]=[j\\e<-a++b%(0,0),i<-['in ':l]&j<-['in ':r l]|e==i]++ $(init(r b))=s

Try it online!

Οurous

Posted 2018-02-08T00:59:36.533

Reputation: 7 916

1

J, 132 bytes

f=:3 :0
c=.(u:97+i.26)-.v=.'aeiou'
d=.u:48+i.10
g=.;"0|.
a=.''
while.*#y do.a=.a,{.y rplc(g c),(g d),g v
y=.|.^:({:a e.v)}.y
end.a
)

Try it online!

A verbose explicit verb this time.

Explanation:

c=.(u:97+i.26) makes a list a-z

v=.'aeiou' makes a list of vowels

-. removes the vowels from the list of letters

d=.u:48+i.10 makes a list of digits

g=.;"0|. a utility verb for creating a list of boxed pairs of replacement symbols

   g d
┌─┬─┐
│0│9│
├─┼─┤
│1│8│
├─┼─┤
│2│7│
├─┼─┤
│3│6│
├─┼─┤
│4│5│
├─┼─┤
│5│4│
├─┼─┤
│6│3│
├─┼─┤
│7│2│
├─┼─┤
│8│1│
├─┼─┤
│9│0│
└─┴─┘

a=.'' a list to store the result

while.*#y do.a=.a,{.y rplc(g c),(g d),g v while the length of the list is > 0 take a symbol, replace it and append it to the result

y=.|.^:({:a e.v)}.y drop one symbol from the beginning of the list and if the symbol is a vowel, reverse the list

end. ends the while loop

a returns the result

Galen Ivanov

Posted 2018-02-08T00:59:36.533

Reputation: 13 815

0

Retina, 78 bytes

T`dl`9-0uz-x\ow-vtis-pnem-j\hagfd-b
/[aeiou]/{*>0L`.*?[aeiou]
0`.*?[aeiou]

V`

Try it online! Link includes test cases. Explanation:

T`dl`9-0uz-x\ow-vtis-pnem-j\hagfd-b

Swap each character with its equivalent.

/[aeiou]/{

Repeat while a vowel remains.

*>0L`.*?[aeiou]

Output the text up to the vowel.

0`.*?[aeiou]

Delete the text up to the vowel.

V`

Reverse the remaining text. When there are no vowels left, this is then implicitly output, however for the purposes of the test cases, the header outputs the text at the end of each line.

Neil

Posted 2018-02-08T00:59:36.533

Reputation: 95 035

0

Stax, 24 bytes

╥j•td╢Ä;Sµ*ûⁿvÉ╫î▓J o╩π╗

Run it

Here is the ascii representation of the same program.

VcGVdGVvGwB]qVvs#!Hv*c}cr\$|t

It translates each character class first, then begins a while loop. In the loop, it outputs the next character, and conditionally reverses the rest of the string if a vowel is encountered.

VcG                             Push lowercase consonants and jump to trailing }
   VdG                          Push digits and jump to trailing }
      VvG                       Push lowercase vowels and jump to trailing }
         wB]qVvs#!Hv*c          While; run this block until popped value is falsy
          B]                    Split first character off string 
            q                   Output with no newline; keep on the stack
             Vvs#               1 if letter is a vowel, 0 otherwise
                 !Hv            Not, Double, then Decrement
                                    -1 for vowels, 1 otherwise
                    *           Multiply string. -1 causes reversal       
                     c          Copy value to be popped as while condition
                      }         Jump target from above.  Return when done.
                       cr\$     Copy, reverse, zip, and flatten.
                           |t   Translate: use string as a character map
                                    for replacements

recursive

Posted 2018-02-08T00:59:36.533

Reputation: 8 616