Decode the hidden message!

11

1

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!

pydude

Posted 2017-04-05T21:04:47.497

Reputation: 777

oh yeah, sorry! editting – pydude – 2017-04-05T21:13:01.987

What about transitivity and circularity, e.g. "massega","e=a","a=e" and the like? – Jonathan Allan – 2017-04-05T21:28:43.447

1bits of decoding info do not decode each other. Only the message is affected by the decoding information. – pydude – 2017-04-05T21:30:09.410

1also, only one replacement will be given for each character, e.g. no "io,"i=u","i=g" – pydude – 2017-04-05T21:32:11.260

1Thanks, I suggest adding this information to the specification. – Jonathan Allan – 2017-04-05T21:33:18.397

Can the input be taken as list of strings in the language's native list format? Like ["a","b","b=c"]. – Rainer P. – 2017-04-05T21:41:10.130

@RainerP. yes, it can. as long as decoding will work if "[" is in one of the strings – pydude – 2017-04-05T21:45:41.907

Is a=bc part of the encoding or part of the message? What about a=? – Titus – 2017-04-06T00:05:05.250

Can output be a newline separated list of characters? – math junkie – 2017-04-06T00:44:03.410

@Titus that would be part of the message, since it is not a=b – pydude – 2017-04-06T00:53:58.943

@math_junkie that would have to be a no. – pydude – 2017-04-06T00:57:39.043

Can we assume the message can never be all equals signs? – Magic Octopus Urn – 2017-04-06T16:42:26.887

Answers

1

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

Jonathan Allan

Posted 2017-04-05T21:04:47.497

Reputation: 67 804

Very interesting, what does m2 "modulo 2 slice" mean? – Magic Octopus Urn – 2017-04-06T17:06:17.247

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.) – Jonathan Allan – 2017-04-06T17:15:33.490

5

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'

vaultah

Posted 2017-04-05T21:04:47.497

Reputation: 1 254

Do you need :-1 or will :2 work? – DSM – 2017-04-06T00:35:38.977

1@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 – vaultah – 2017-04-06T00:43:34.387

1Ahh, good point! – DSM – 2017-04-06T00:44:31.173

2

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.

Rainer P.

Posted 2017-04-05T21:04:47.497

Reputation: 2 457

2

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

ASCII-only

Posted 2017-04-05T21:04:47.497

Reputation: 4 687

1

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.

math junkie

Posted 2017-04-05T21:04:47.497

Reputation: 2 490

0

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.)

Neil

Posted 2017-04-05T21:04:47.497

Reputation: 95 035

0

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

Jörg Hülsermann

Posted 2017-04-05T21:04:47.497

Reputation: 13 026

0

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.

Titus

Posted 2017-04-05T21:04:47.497

Reputation: 13 814

0

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.

Magic Octopus Urn

Posted 2017-04-05T21:04:47.497

Reputation: 19 422