15
\$\begingroup\$

Note

This is the encryption challenge. The decryption challenge can be found here.

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!

\$\endgroup\$
7
  • 1
    \$\begingroup\$ Step 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. \$\endgroup\$
    – dylnan
    Feb 8, 2018 at 1:11
  • \$\begingroup\$ @dylnan Added that. \$\endgroup\$ Feb 8, 2018 at 1:14
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – Emigna
    Feb 8, 2018 at 10:00
  • 4
    \$\begingroup\$ There will soon be a decryption challenge for the same, in which I will try to explain the process \$\endgroup\$ Feb 8, 2018 at 10:38
  • 1
    \$\begingroup\$ For the curious, in Indian English alphabet means letter. \$\endgroup\$
    – TRiG
    Feb 8, 2018 at 11:22

8 Answers 8

4
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ žhžMžN)UvćXJXíJ‡D?žMsåiR was what I was thinking for improvement, but can't cut XJXiJ down enough. \$\endgroup\$ Feb 8, 2018 at 14:34
  • \$\begingroup\$ @MagicOctopusUrn: I had a similar idea with DJsíJ which wasn't very effective either. \$\endgroup\$
    – Emigna
    Feb 8, 2018 at 15:02
4
\$\begingroup\$

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):"")
\$\endgroup\$
5
  • \$\begingroup\$ & didn't work for some numbers but && worked. Thanks. \$\endgroup\$ Feb 8, 2018 at 7:56
  • \$\begingroup\$ Oh yes, I didn't find a method to optimize that and you did it! Thanks! \$\endgroup\$ Feb 8, 2018 at 8:07
  • 3
    \$\begingroup\$ 124 bytes by using the same string for all letters and applying some more golfing. \$\endgroup\$
    – Arnauld
    Feb 8, 2018 at 9:41
  • \$\begingroup\$ Wow brilliant! I didn't think of combining the strings AT ALL \$\endgroup\$ Feb 8, 2018 at 10:01
  • \$\begingroup\$ q=="0"|+q is actually 1 byte shorter than q>" "&&1/q. \$\endgroup\$
    – Arnauld
    Feb 8, 2018 at 11:31
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

Brachylog, 43 39 bytes

hX∧Ḅ;Ṿ,Ị,Ṣ∋C↔;Cz∋[X,H]&{h∈Ṿ&b↔|b}↰↔,H↔|

Try it online!

I only bothered finishing this grand mess because I'm struggling to make Jelly cooperate...

\$\endgroup\$
0
\$\begingroup\$

Retina, 78 69 bytes

T`b-df-hj-maed\oup-tv-z`Ro
/[aeiou]/{*>0L`.*?[aeiou]
0`.*?[aeiou]

V`

Try it online! Link includes test cases. Explanation:

T`b-df-hj-maed\oup-tv-z`Ro

Swap each character with its equivalent. The Ro causes the string to transliterate to its reverse. The middle consonant n and vowel i map to themselves so don't need to be listed. The first 10 consonants are listed first and the last 10 last so that they transliterate to each other. The first and last two vowels then surround the digits. (The o is normally special so it has to be quoted here.)

/[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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.