Decode the string

2018-04-22 23:45:08

This is my first challenge on ppcg!

Input

A string consisting of two different ascii characters. For example

ABAABBAAAAAABBAAABAABBAABA

Challenge

The task is to decode this string following these rules:

Skip the first two characters

Split the rest of the string into groups of 8 characters

In each group, replace each character with 0 if that character is the same as the first character of the original string, and with 1 otherwise

Now each group represents a byte. Convert each group to character from byte char code

Concatenate all characters

Example

Let's decode the above string.

AB AABBAAAA AABBAAAB AABBAABA

^^ ^ ^ ^

| | | |

| \---------|---------/

| |

Skip Convert to binary

Notice that A is the first character in the original string and B is the second. Therefore, replace each A with 0 and each B with 1. Now we obtain:

00110000 00110001 00110010

which is [0x30, 0x31, 0x32] in bina

  • J, 17 Bytes

    u:_8#.\2&({.i.}.)

    Explanation:

    u:_8#.\2&({.i.}.)

    2&({.i.}.) | Index of each item of the rest of the string in the first two items

    _8#.\ | Convert non-intersecting subarrays of length 8 from binary

    u: | Convert to characters

    Examples:

    2&({. ; }.) 'ABAABBAAAAAABBAAABAABBAABA'

    ┌──┬────────────────────────┐

    │AB│AABBAAAAAABBAAABAABBAABA│

    └──┴────────────────────────┘

    2&({.i.}.) 'ABAABBAAAAAABBAAABAABBAABA'

    0 0 1 1 0 0 0 0 0 0 1 1 0 0 0 1 0 0 1 1 0 0 1 0

    _8#.\2&({.i.}.) 'ABAABBAAAAAABBAAABAABBAABA'

    48 49 50

    u:_8#.\2&({.i.}.) 'ABAABBAAAAAABBAAABAABBAABA'

    012

    2018-04-23 00:49:14
  • Python 2, 88 bytes

    lambda s:[chr(int(''.join('01'[c==s[1]]for c in t),2))for t in(s[2:10],s[10:18],s[18:])]

    Try it online!

    2018-04-23 01:02:15