11
\$\begingroup\$

Introduction

One day, you were just relaxing in your office in the CIA, when suddenly you see an alert on your computer. Your programs have just intercepted hundreds of coded messages! A quick examination reveals the rule for encoding, but you need a program in order to decode fast.

Challenge

You will be given a list of strings, separated by commas. Each string will contain either:

  • Part of the coded message
    • It is part of the coded message if it is not in the form a=b. Note that it is part of the message if it is ab=c. Add this string to the coded message.
  • Part of the encoding scheme
    • This will be in the form of a=b. That means that all a's in the message must be replaced by b's. Note that it could be a==, meaning that all a`s must be replaced with ='s.

Your program must then output the message, decoded using the scheme found.

Other info: Your input will only contain commas for separating the strings. It could contain other characters, like !1#, etc. It will not contain uppercase letters. Bits of decoding info do not decode each other. Only the message is affected by the decoding information. Only one replacement will be given for each character, e.g. no "io,"i=u","i=g"

Examples

Input:"ta","y=s","y","a=e","b=t","b"," ","j","j=1"

Output:test 1

Input:"z=p","zota","g=e","yugkb","y=t","u=o","k=s","li","fg","b=="

Output:potatoes=life

Input:"p","=","==n","ot","p=a","hiz","i=e","z=r"

Output:another

This is , so shortest answer in bytes wins!

\$\endgroup\$
  • \$\begingroup\$ oh yeah, sorry! editting \$\endgroup\$ – pydude Apr 5 '17 at 21:13
  • \$\begingroup\$ What about transitivity and circularity, e.g. "massega","e=a","a=e" and the like? \$\endgroup\$ – Jonathan Allan Apr 5 '17 at 21:28
  • 1
    \$\begingroup\$ bits of decoding info do not decode each other. Only the message is affected by the decoding information. \$\endgroup\$ – pydude Apr 5 '17 at 21:30
  • 1
    \$\begingroup\$ also, only one replacement will be given for each character, e.g. no "io,"i=u","i=g" \$\endgroup\$ – pydude Apr 5 '17 at 21:32
  • 1
    \$\begingroup\$ Thanks, I suggest adding this information to the specification. \$\endgroup\$ – Jonathan Allan Apr 5 '17 at 21:33
1
\$\begingroup\$

Jelly, 19 bytes

ḊṖ⁼“=”
ÇÐfKm2yÇÐḟF$

Try it online!

How?

ḊṖ⁼“=” - Link 1, isMappngElement?: string
Ḋ      - dequeue
 Ṗ     - pop
   “=” - char-list,['=']
  ⁼    - equal?

ÇÐfKm2yÇÐḟF$ - Main link: list of strings
 Ðf          - filter keep:
Ç            -     last link (1) as a monad
   K         - join with spaces
    m2       - modulo 2 slice (every other character)
           $ - last two links as a monad:
        Ðḟ   -     filter discard:
       Ç     -         last link (1) as a monad
          F  -     flatten
      y      - translate right according to the mapping of left
\$\endgroup\$
  • \$\begingroup\$ Very interesting, what does m2 "modulo 2 slice" mean? \$\endgroup\$ – Magic Octopus Urn Apr 6 '17 at 17:06
  • \$\begingroup\$ m is a dyadic atom that takes every right-th element of left*. Here, for example, ['x','=','y','<space>','a','=','b']m2 would yield ['x','y','a','b']. (* unless right is zero when it appends a reflection instead.) \$\endgroup\$ – Jonathan Allan Apr 6 '17 at 17:15
5
\$\begingroup\$

Python 3, 98

lambda l:''.join(x*('='!=x[1:-1])for x in l).translate({'='!=x[1:-1]or ord(x[0]):x[2:]for x in l})

This lambda function receives a list of strings (input) and returns a string (the decoded message).

Examples:

>>> f(['ta', 'y=s', 'y', 'a=e', 'b=t', 'b', ' ', 'j', 'j=1'])
'test 1'
>>> f(['z=p', 'zota', 'g=e', 'yugkb', 'y=t', 'u=o', 'k=s', 'li', 'fg', 'b=='])
'potatoes=life'
>>> f(['p', '=', '==n', 'ot', 'p=a', 'hiz', 'i=e', 'z=r'])
'another'
\$\endgroup\$
  • \$\begingroup\$ Do you need :-1 or will :2 work? \$\endgroup\$ – DSM Apr 6 '17 at 0:35
  • 1
    \$\begingroup\$ @DSM I think it's needed because '=' == x[1:2] will be true for x = 'a=bc', which is not a part of the encoding scheme \$\endgroup\$ – vaultah Apr 6 '17 at 0:43
  • 1
    \$\begingroup\$ Ahh, good point! \$\endgroup\$ – DSM Apr 6 '17 at 0:44
2
\$\begingroup\$

Haskell, 85 bytes

f x=[(a,b)|[a,'=',b]<-x]
h x=map(\v->maybe v id$lookup v$f x)$concat[c|c<-x,[]==f[c]]

Usage

>h ["p","=","==n","ot","p=a","hiz","i=e","z=r"]
>"another"

Description

f creates a lookup table.

concat[c|c<-x,[]==f[c]] extracts the message.

map(\v->maybe v id$lookup v$f x) perfoms the lookup.

\$\endgroup\$
2
\$\begingroup\$

JavaScript (ES6), 87 bytes

(l,s='',d={})=>l.map(v=>/.=./.test(v)?d[v[0]]=v[2]:s+=v)&&[...s].map(c=>d[c]||c).join``

<input id=a oninput="try{b.innerText=((l,s='',d={})=>l.map(v=>/.=./.test(v)?d[v[0]]=v[2]:s+=v)&&[...s].map(c=>d[c]||c).join``)(eval(`[${a.value}]`))}catch(e){}"/>
<p id=b />

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

Retina, 84 82 77 74 bytes

Takes a comma-separated list as input. Note the trailing newline

^|$
,,
+`,(.=.)(,.*)
$2$1
M!&`(.).*,,.*\1=.|.+,
%`(.).*,$|.*(.)
$1$2
\n|,

Try it Online!

Explanation:

First, we move all expressions of the form .=. to the end of the string and separate these from the message with a double comma (,,). This is so that in the next step, we can find all encodings by checking if each character ahead of the ,, has a matching =. afterwards. This is achieved by M!&`(.).*,,.*\1=.|.+, which finds all such matches and places them into a linefeed separated list of strings. We then modify each string to only contain either one unencoded character or the encoded version of the character. Finally we replace all linefeeds and commas with the empty string so that our output is formatted nicely.

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

Batch, 188 bytes

@echo off
set/pm=
set c=
for %%s in (%m%)do call:c %%s "%%c%%%%~s" not
for %%s in (%m%)do call:c %%s "%%c:%%~s%%"
echo %c%
exit/b
:c
set s=%~1
if %3 "%s:~1,1%"=="=" call set c=%~2

Explanation: Loops through the list of strings twice (conveniently for likes a string in CSV format). The first time, looks for strings that do not contain an = as the second character, and concatenates them to the result. The second time, looks for strings that do contain an = as the second character, and performs the substitution. (even more conveniently, the substitution is already in Batch format.)

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

PHP, 116 Bytes

<?foreach($_GET as$i)$i[1]=="="&&strlen($i)==3?$r[]=$i:$s.=$i;foreach($r as$l)$s=str_replace($l[0],$l[2],$s);echo$s;

Online Version

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

PHP, 89 87 bytes

two versions:

while(a&$s=$argv[++$i])3==strlen($s)&"="==$s[1]?$t[$s[0]]=$s[2]:$m.=$s;echo strtr($m,$t);
while(a&$s=$argv[++$i])preg_match("#^.=.$#",$s)?$t[$s[0]]=$s[2]:$m.=$s;echo strtr($m,$t);

takes input from command line arguments; run with -nr.

  • loop through arguments creating parameters for strtr
    (translation if argument contains =, message else).
  • perform strtr.
\$\endgroup\$
0
\$\begingroup\$

05AB1E, 31 bytes

vy'=åyg3Q&iyˆyS}})øJÁ¨`¹¯KJ.Ás‡

Try it online!

vy              }
  '=åyg3Q&                      # See if it's length 3 with an equals in the mid.
          iyˆyS                 # If so, add to global array, split into chars.
               } 
                 )øJÁ¨`         # Transpose, join, and remove the middle =.
                       ¹¯KJ     # Push original input without decryption key.
                           .Ás‡ # Shift stack to the right, swap and transliterate.
\$\endgroup\$

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.