6
\$\begingroup\$

Back-story

After typing up your essay on your new Macbook, you look up at the screen and realize that you typed in ten pages of gibberish! Or is it? On further inspection, you come to find that your Option key was stuck this whole time and these special Option-key characters correspond to the ASCII keys on your keyboard!

Challenge

Your challenge is to write the shortest possible program or function that converts an encoded Option-key input into ASCII output.

Here's the Option-key special characters to ASCII characters dictionary:

+---------+-------+-------+-------+
| OPT-KEY |  CODE | ASCII |  CODE |
+---------+-------+-------+-------+
|    ¡    |   161 |   1   |    49 |
|    ™    |  8482 |   2   |    50 |
|    £    |   163 |   3   |    51 |
|    ¢    |   162 |   4   |    52 |
|    ∞    |  8734 |   5   |    53 |
|    §    |   167 |   6   |    54 |
|    ¶    |   182 |   7   |    55 |
|    •    |  8226 |   8   |    56 |
|    ª    |   170 |   9   |    57 |
|    º    |   186 |   0   |    48 |
|    –    |  8211 |   -   |    45 |
|    ≠    |  8800 |   =   |    61 |
|    œ    |   339 |   q   |   113 |
|    ∑    |  8721 |   w   |   119 |
|    ®    |   174 |   r   |   114 |
|    †    |  8224 |   t   |   116 |
|    ¥    |   165 |   y   |   121 |
|    ø    |   248 |   o   |   111 |
|    π    |   960 |   p   |   112 |
|    “    |  8220 |   [   |    91 |
|    ‘    |  8216 |   ]   |    93 |
|    «    |   171 |   \   |    92 |
|    å    |   229 |   a   |    97 |
|    ß    |   223 |   s   |   115 |
|    ∂    |  8706 |   d   |   100 |
|    ƒ    |   402 |   f   |   102 |
|    ©    |   169 |   g   |   103 |
|    ˙    |   729 |   h   |   104 |
|    ∆    |  8710 |   j   |   106 |
|    ˚    |   730 |   k   |   107 |
|    ¬    |   172 |   l   |   108 |
|    …    |  8230 |   ;   |    59 |
|    æ    |   230 |   '   |    39 |
|    Ω    |   937 |   z   |   122 |
|    ≈    |  8776 |   x   |   120 |
|    ç    |   231 |   c   |    99 |
|    √    |  8730 |   v   |   118 |
|    ∫    |  8747 |   b   |    98 |
|    µ    |   181 |   m   |   109 |
|    ≤    |  8804 |   ,   |    44 |
|    ≥    |  8805 |   .   |    46 |
|    ÷    |   247 |   /   |    47 |
|         |   160 |       |    32 |
|    ⁄    |  8260 |   !   |    33 |
|    €    |  8364 |   @   |    64 |
|    ‹    |  8249 |   #   |    35 |
|    ›    |  8250 |   $   |    36 |
|    fi    | 64257 |   %   |    37 |
|    fl    | 64258 |   ^   |    94 |
|    ‡    |  8225 |   &   |    38 |
|    °    |   176 |   *   |    42 |
|    ·    |   183 |   (   |    40 |
|    ‚    |  8218 |   )   |    41 |
|    —    |  8212 |   _   |    95 |
|    ±    |   177 |   +   |    43 |
|    Π   |   338 |   Q   |    81 |
|    „    |  8222 |   W   |    87 |
|    ‰    |  8240 |   R   |    82 |
|    ˇ    |   711 |   T   |    84 |
|    Á    |   193 |   Y   |    89 |
|    Ø    |   216 |   O   |    79 |
|    ∏    |  8719 |   P   |    80 |
|    ”    |  8221 |   {   |   123 |
|    ’    |  8217 |   }   |   125 |
|    »    |   187 |   |   |   124 |
|    Å    |   197 |   A   |    65 |
|    Í    |   205 |   S   |    83 |
|    Î    |   206 |   D   |    68 |
|    Ï    |   207 |   F   |    70 |
|    ˝    |   733 |   G   |    71 |
|    Ó    |   211 |   H   |    72 |
|    Ô    |   212 |   J   |    74 |
|        | 63743 |   K   |    75 |
|    Ò    |   210 |   L   |    76 |
|    Ú    |   218 |   :   |    58 |
|    Æ    |   198 |   "   |    34 |
|    ¸    |   184 |   Z   |    90 |
|    ˛    |   731 |   X   |    88 |
|    Ç    |   199 |   C   |    67 |
|    ◊    |  9674 |   V   |    86 |
|    ı    |   305 |   B   |    66 |
|    Â    |   194 |   M   |    77 |
|    ¯    |   175 |   <   |    60 |
|    ˘    |   728 |   >   |    62 |
|    ¿    |   191 |   ?   |    63 |
+---------+-------+-------+-------+

It's not that simple though... There are a five Option-key special characters that do not follow this trend, specifically (Option-key special characters and their corresponding lowercase ASCII characters):

Beginning of input or after special space (" ", code 160):

+---------+-------+-------+-------+
| OPT-KEY |  CODE | ASCII |  CODE |
+---------+-------+-------+-------+
|    `    |    96 |   ~   |   126 |
|    ´    |   180 |   E   |    69 |
|    ¨    |   168 |   U   |    85 |
|    ˆ    |   710 |   I   |    73 |
|    ˜    |   732 |   N   |    78 |
+---------+-------+-------+-------+

Otherwise:

+---------+-------+-------+-------+
| OPT-KEY |  CODE | ASCII |  CODE |
+---------+-------+-------+-------+
|    `    |    96 |   `   |    96 |
|    ´    |   180 |   e   |   101 |
|    ¨    |   168 |   u   |   117 |
|    ˆ    |   710 |   i   |   105 |
|    ˜    |   732 |   n   |   110 |
+---------+-------+-------+-------+

When any of these characters are found after the Option-key special space character ( ) or at the beginning of the input, output the capitalized version of their ASCII equivalents. (Note that the capitalized version of "`" is "~".) Otherwise, output the lowercase version of their ASCII equivalents.

Finally, do not attempt to decode any other characters, just output them.

Rules & Grading

  • All Code-Golf rules barring standard loopholes apply.
  • Scores will be based on the number of bytes your program uses.
  • The program with the smallest score (or the smallest number of bytes) will win!

Examples

Input, output:

`´¨ˆ˜`
~euin`

´´ ´⁄
Ee E!

`` ´ ¨ ˆ ˜
~` E U I N

Ó´¬¬ø ∑ø®¬∂⁄
Hello world!

„˙冠ˆß ¥ø¨® ˜åµ´¿      Í´®©´å˜† ıˆ¬¬ˆå®∂ß⁄
What Is your Name?      Sergeant Billiards!

„˙´®´ ∂ø ¥ø¨ çøµ´ ƒ®øµ¿
Where do you come from?

„˙´˜ ˆ µø√´∂ †ø Áø®˚ß˙ˆ®´≤ †˙´®´ ∑åߠ堵嘠∑分ˆ˜© ƒø® µ´≥
When I moved to Yorkshire, there was a man waiting for me.

ӈߠ˜åµ´ ∑åß ∏嬴߆ˆ˜´≥ Ó´ çåµ´ ƒ®øµ Òå˜∂ø≥
His Name was Palestine. He came from Lando.

Åç®øßß †˙´ ®´´ƒ ¬å¥ ˜ø†˙ˆ˜©≥
Across the reef lay Nothing.

Â¥ ˜åµ´ ˆß ´å†ø˜≥
My Name Is Eaton.

ˆ˜† µåˆ˜·√øˆ∂‚ ” ß†∂ÚÚçø¨† ¯¯ ÆÓ´¬¬ø ∑ø®¬∂⁄Æ… ’
Int main(void) { std::cout << "Hello world!"; }

¡ ·™£¢‚ ∞§¶–•ª¡º
1 (234) 567-8910

ÓʼnΠˇØ ˝´ˇ ÅÒÒ ÇÅ∏Í⁄
HARD TO GeT ALL CAPS!

؉ ˆÍ ˆˇ¿
OR IS IT?

Ó´æß ˆ˜ †˙´ †øπ ¡ººfi⁄
He's In the top 100%!
\$\endgroup\$
  • 3
    \$\begingroup\$ @cat I had a better one before but those rationales are considered bad. \$\endgroup\$ – usandfriends Jan 19 '16 at 4:59
  • 5
    \$\begingroup\$ I dislike that they are considered bad :( \$\endgroup\$ – cat Jan 19 '16 at 5:00
  • 1
    \$\begingroup\$ @usandfriends well, I hate Apple but that's not what's keeping me from answering :) \$\endgroup\$ – cat Jan 20 '16 at 23:06
  • 1
    \$\begingroup\$ @user81655 Á´π≤ ƒˆ≈´∂ †˙å†≥ ˆ ˙å√´ ˜ø ˆ∂´å ∑˙å† ˆ ∑åß ∂øˆ˜© ∑˙´˜ ˆ ¨π¬øå∂´∂ †˙ˆß≥≥≥ ˇ˙å˜˚ß ƒø® ¬´††ˆ˜© µ´ ˚˜ø∑⁄ \$\endgroup\$ – usandfriends Jan 21 '16 at 0:22
  • 1
    \$\begingroup\$ ˜ø π®ø∫¬´µ≥ Ú‚ \$\endgroup\$ – user81655 Jan 21 '16 at 0:31
2
\$\begingroup\$

JavaScript (ES6), 354 bytes (231 characters)

t=>[...t].map((c,i)=>~(x="`´¨ˆ˜".indexOf(c))?(t[i-1]==" "|!i?"~EUIN":"`euin")[x]:~(a=" ⁄`‹›fi‡`·‚°±≤–≥÷º¡™£¢∞§¶•ªÚ…¯≠˘¿€ÅıÇÎ`Ï˝Ó`ÔÒÂ`Ø∏Œ‰Íˇ`◊„˛Á¸“«‘fl—`å∫ç∂`ƒ©˙`∆˚¬µ`øπœ®ß†`√∑≈¥Ω”»’æÆ".indexOf(c))?String.fromCharCode(a+32):c).join``

Explanation

Note: This byte counter says it is 278 bytes, but I don't know what (if any) encoding it is using, so I went with UTF-8 for the byte count.

t=>
  [...t].map((c,i)=>                      // for each character c at index i
    ~(x="`´¨ˆ˜".indexOf(c))?              // if it is a special character
      (t[i-1]==" "|!i?"~EUIN":"`euin")[x] // handle it
    :~(
      a=                                  // a = ASCII code of equivalent character (or -1)

        // Option characters, the index of each option character is the code of it's ASCII
        //     equivalent - 32, ` is used to pad empty positions since it will never match
        " ⁄`‹›fi‡`·‚°±≤–≥÷º¡™£¢∞§¶•ªÚ…¯≠˘¿€ÅıÇÎ`Ï˝Ó`ÔÒÂ`Ø∏Œ‰Íˇ`◊„˛Á¸“«‘fl—`å∫ç∂`ƒ©˙`∆˚¬µ`øπœ®ß†`√∑≈¥Ω”»’æÆ"

        .indexOf(c)
    )?String.fromCharCode(a+32)           // return the ASCII character if an option
                                          //     character was matched
    :c                                    // else return the character unchanged
  )
  .join``                                 // return output

Test

var solution = t=>[...t].map((c,i)=>~(x="`´¨ˆ˜".indexOf(c))?(t[i-1]==" "|!i?"~EUIN":"`euin")[x]:~(a=" ⁄Æ‹›fi‡æ·‚°±≤–≥÷º¡™£¢∞§¶•ªÚ…¯≠˘¿€ÅıÇÎ`Ï˝Ó`ÔÒÂ`Ø∏Œ‰Íˇ`◊„˛Á¸“«‘fl—`å∫ç∂`ƒ©˙`∆˚¬µ`øπœ®ß†`√∑≈¥Ω”»’".indexOf(c))?String.fromCharCode(a+32):c).join``
<input type="text" id="input" value="„˙´˜ ˆ µø√´∂ †ø Áø®˚ß˙ˆ®´≤ †˙´®´ ∑åߠ堵嘠∑分ˆ˜© ƒø® µ´≥" />
<button onclick="result.textContent=solution(input.value)">Go</button>
<pre id="result"></pre>

\$\endgroup\$
  • \$\begingroup\$ Nice! There's only one problem with your solution. You are checking for the normal (code 32) ASCII space rather than the non-breaking space (code 160). Also, in your long ASCII string, it looks like you are using the same (code 32) ASCII space. I think this is happening because when you copy that character, your browser/OS/something converts it to a normal ASCII space. (That's my guess at least...) \$\endgroup\$ – usandfriends Jan 21 '16 at 0:33
  • \$\begingroup\$ @usandfriends It's Stack Exchange's fault. It's also removed from yours in the question by the way. \$\endgroup\$ – user81655 Jan 21 '16 at 0:37
  • 1
    \$\begingroup\$ FYI, the byte counter you're referring to is encoded in ISO-8859-1. \$\endgroup\$ – Mama Fun Roll Jan 21 '16 at 0:42
  • 2
    \$\begingroup\$ @ՊՓԼՃՐՊՃՈԲՍԼ That's a single byte encoding which doesn't support most of these characters. So it was trying to trick me! \$\endgroup\$ – user81655 Jan 21 '16 at 0:47
  • \$\begingroup\$ @usandfriends It's showing as a normal space in the question for me... \$\endgroup\$ – user81655 Jan 21 '16 at 0:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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