r/dailyprogrammer • u/Coder_d00d 1 3 • Jun 03 '15
[2015-06-03] Challenge #217 [Intermediate] Space Code Breaking
Description:
The year 2266 we have encountered alien planets who use very simple encryption to send messages. Lucky for us we intercept all these messages and we can break the code.
The problem is the collection of messages are all from the same space probe. So we are not sure which message is from what system.
Our challenge today is to decode the message and have our solutions determine which planet system the message came from.
Edit note:
Copying my ASCII data over as input is causing problems. I see that some people who were true heroes and tackled the problem early are seeing this. To fix this we will be altering the challenge. Input will be a set of numbers each represent a byte in the message. Hopefully this will fix the issues.
Input:
Message broken down into numbers representing the ASCII values of the message between " "
Output:
The name of the system and the message decoded.
Encryption and Planet Systems:
Omicron V: will take and invert the 5th bit. ( 0001 0000) That is the bit location in the byte where we invert the bit.
Hoth: Takes the value of the ASCII character and adds 10 to it.
Ryza IV: Takes the value of the ASCII character and subtracts 1 to it.
Htrae: reverses the characters.
Validation:
It is not enough to just take the message and decode it in all 4 ways and let you decide which one is right or wrong. You need to have your program/solution determine the right decoding. All messages are in english (I know even in the future on alien planets).
Example:
input:
" 101 99 97 101 112 32 110 105 32 101 109 111 99 32 101 87 "
Note:
This would be "ecaeP ni emoc eW" in displayed ascii - some messages don't display well as the values take them beyond displayable ascii values (thus the decimal values)
output:
Htrae: We come in Peace
Challenge Input:
" 71 117 48 115 127 125 117 48 121 126 48 96 117 113 115 117 "
" 97 111 42 109 121 119 111 42 115 120 42 122 111 107 109 111 "
" 86 100 31 98 110 108 100 31 104 109 31 111 100 96 98 100 "
" 101 99 97 101 112 32 110 105 32 101 109 111 99 32 101 87 "
" 84 113 121 124 105 48 64 98 127 119 98 113 125 125 117 98 48 121 99 48 99 96 105 121 126 119 48 127 126 48 101 99 "
" 78 107 115 118 131 42 90 124 121 113 124 107 119 119 111 124 42 115 125 42 125 122 131 115 120 113 42 121 120 42 127 125 "
" 67 96 104 107 120 31 79 113 110 102 113 96 108 108 100 113 31 104 114 31 114 111 120 104 109 102 31 110 109 31 116 114 "
" 115 117 32 110 111 32 103 110 105 121 112 115 32 115 105 32 114 101 109 109 97 114 103 111 114 80 32 121 108 105 97 68 "
" 86 121 98 117 48 100 120 117 48 93 121 99 99 124 117 99 "
" 80 115 124 111 42 126 114 111 42 87 115 125 125 118 111 125 "
" 69 104 113 100 31 115 103 100 31 76 104 114 114 107 100 114 "
" 115 101 108 115 115 105 77 32 101 104 116 32 101 114 105 70 "
Challenge Solution:
The 12 messages are 3 messages in each of the 4 encodings. Hopefully you should come up with
"We come in Peace"
"Daily Programmer is spying on us"
"Fire the missiles"
in all of the 4 encodings each.
View all comments
Show parent comment
3
u/Ledrug 0 2 Jun 04 '15
Depending on how familiar you are with Perl, the code can be pretty hard to understand, and the explanations can be very long. First, the basics:
map(some_code, a_list_of_things): here some_code may operate on $_; for each item in a_list_of_things, it gets assigned to $_ and then some_code runs; map collects each such operation's result in a new list and returns it. Since parentheses are optional when unambiguous (to Perl, not necessarily to human), it can also be written as
grep(some_code, a_list_of_thins): similiar to map, but result of some_code operating on each $_ is treated as true/false here, and those that are true gets returned by grep. It is a filter that only lets the list items satisfying some_code to pass through.
Note that another, probably more common, form for map and grep is
where in between the curly brackets is a code block that works like a function body. You can do more complicated stuff this way.
Now on to the code itself. The program is best read backwards:
print weird_looking_stuff for: don't worry about stuff between print and for yet. The basic feature here is
which takes each item in @list, assign it to (wait for it) $_, and runs some_code. Unlike map though, nothing is collected and returned.
The print takes everything after it and, well, prints them. The first item is
the "\n>> " and "\n" are literal characters; the "$_" is, as you remember, a reference to a list of numbers on the same row. The "@" unwraps the reference and turns it back into a list which gets printed.
But print also prints out the rest of its argument, that is, whatever produced by
Let's see what it does. At the bottom 4 list references are produced; for example
The "=>" is just a fancy comma, to whose left an unambiguous (to Perl) token can be interpreted as a literal string without quotes; the rest is again a map operation, which takes $_ (remember, the reference to the list of numbers), turns it back to list ("@$_"), and produces "$_ + 1" to each item (this $_ is different from the previous one!), that is, a new list whose items are 1 greater than the input numbers. In plain English, each square bracket pair takes the input row, produces a list of decoded numbers, and prefix it with the name of the decoder. These 4 decoding results are then each seen as $_ by the
map shift(@$_).": ".pack("C", @$_, 10)*, ...: each input $_ is taken, turned back into the list it was, first item (the decoder name prefix) removed, the rest suffixed by a "10" (ASCII code for newline), then converted from ASCII code to char string (what pack does in "C*" mode). The decoder name is then joined back to the string at the begining with a colon. At this stage, each row of original numbers is turned into 4 lines of text, in the format of
The 4 lines are then fed to:
grep /^[\w\s:]+$/, ...: each of the 4 lines gets tested by the regex in the bold face, which checks if the line contains only letters, digits, underscores, whitespaces, and colons (because we added one). Lines containing anything else are dropped by grep. Granted this is not a reliable filter, for example we'd squelch the aliens if they had properly punctuated their sentences. Then again, without a well defined constraint of what they might be transmitting, there's no reliable way to determine the validity of translations. What if Omicron Vians wanted to send us Perl code snippets, huh? There then might be nothing but random punctuations!
Anyhow, whatever strings that survived the grep filter are seen by print and printed to screen, and -- Wow, you are still here? -- we are done.