28
0
The goal of this challenge is the write the shortest function/program to take input text, encrypt it using the method below, and return the result.
As an example, I will use the string hello world
.
First, get the input text.
hello world
Second, convert the string into ternary (base 3). Use this key:
a = 000
b = 001
c = 002
d = 010
e = 011
f = 012
g = 020
...
w = 211
x = 212
y = 220
z = 221
[space] = 222
With this key, hello world
becomes 021011102102112222211112122102010
, as seen below.
h e l l o w o r l d
021 011 102 102 112 222 211 112 122 102 010
Third, move the first digit to the end.
021011102102112222211112122102010
becomes
210111021021122222111121221020100
Fourth, convert the number back into a string using the same key.
210 111 021 021 122 222 111 121 221 020 100
v n h h r n q z g j
Lastly, return the encrypted text.
vnhhr nqzgj
Here are some sample text and their output:
the turtle ==> dvo fh ego
python ==> uudwqn
code golf ==> hpjoytqgp
As this is code golf, the shortest entry in bytes wins. Errors are allowed if some of the characters are not lowercase letters or space. This is my first challenge, so any suggestions would be more than helpful.
Good luck!
Leaderboard:
var QUESTION_ID=54643;function answersUrl(e){return"http://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),e.has_more?getAnswers():process()}})}function shouldHaveHeading(e){var a=!1,r=e.body_markdown.split("\n");try{a|=/^#/.test(e.body_markdown),a|=["-","="].indexOf(r[1][0])>-1,a&=LANGUAGE_REG.test(e.body_markdown)}catch(n){}return a}function shouldHaveScore(e){var a=!1;try{a|=SIZE_REG.test(e.body_markdown.split("\n")[0])}catch(r){}return a}function getAuthorName(e){return e.owner.display_name}function process(){answers=answers.filter(shouldHaveScore).filter(shouldHaveHeading),answers.sort(function(e,a){var r=+(e.body_markdown.split("\n")[0].match(SIZE_REG)||[1/0])[0],n=+(a.body_markdown.split("\n")[0].match(SIZE_REG)||[1/0])[0];return r-n});var e={},a=1,r=null,n=1;answers.forEach(function(s){var t=s.body_markdown.split("\n")[0],o=jQuery("#answer-template").html(),l=(t.match(NUMBER_REG)[0],(t.match(SIZE_REG)||[0])[0]),c=t.match(LANGUAGE_REG)[1],i=getAuthorName(s);l!=r&&(n=a),r=l,++a,o=o.replace("{{PLACE}}",n+".").replace("{{NAME}}",i).replace("{{LANGUAGE}}",c).replace("{{SIZE}}",l).replace("{{LINK}}",s.share_link),o=jQuery(o),jQuery("#answers").append(o),e[c]=e[c]||{lang:c,user:i,size:l,link:s.share_link}});var s=[];for(var t in e)e.hasOwnProperty(t)&&s.push(e[t]);s.sort(function(e,a){return e.lang>a.lang?1:e.lang<a.lang?-1:0});for(var o=0;o<s.length;++o){var l=jQuery("#language-template").html(),t=s[o];l=l.replace("{{LANGUAGE}}",t.lang).replace("{{NAME}}",t.user).replace("{{SIZE}}",t.size).replace("{{LINK}}",t.link),l=jQuery(l),jQuery("#languages").append(l)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",answers=[],page=1;getAnswers();var SIZE_REG=/\d+(?=[^\d&]*(?:<(?:s>[^&]*<\/s>|[^&]+>)[^\d&]*)*$)/,NUMBER_REG=/\d+/,LANGUAGE_REG=/^#*\s*([^,]+)/;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"><div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table></div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table></div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody></table><table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody></table>
3Note that this technically is obfuscation, not encryption. There is no encryption key here. – John Dvorak – 2015-08-14T08:43:44.800
@JanDvorak Will "cipher" work? – The Turtle – 2015-08-14T13:55:00.300
To build on @JanDvorak's comment, I would describe this as an "encoding," which is a standard way of expressing data in a different format (e.g., you can express a bit string with hexadecimal encoding, or base64 encoding). – apsillers – 2015-08-14T19:26:47.297
@apsillers Except now you're re-encoding into the same alphabet as the source. For no reason other than obfuscation^K code golf challenge :-D – John Dvorak – 2015-08-14T19:51:30.720
1
@JanDvorak Agreed -- I'd be more likely to call ROT13 "obfuscation" rather than "encoding" (even though it satisfies both definitions). Probably the most precise title would be "Obfuscate text by..."
– apsillers – 2015-08-14T19:54:59.373@apsillers Okay, thanks! Changed. – The Turtle – 2015-08-14T20:55:10.100
Can we assume some limit as to the string length? Or perhaps you cauld define some penalty (add some number to the byte count) for code that only works for short strings. I have a method in mind for strings up to 10 or 11 characters; maybe others can come up with efficient approaches for short strings too – Luis Mendo – 2015-08-14T22:32:41.233
@LuisMendo I guess the limit is about how much the programming language can handle. (I know that is kind of vague, but I don't think a programming language would have a 11-character limit) I would like to see the program though! (maybe here on the comments?) – The Turtle – 2015-08-14T23:16:46.960
2@TheTurtle The idea was to pack the characters into a large integer (
uint64
) using increasing powers of27
. The displacement by one digit would then be equivalent to multiplying this large integer by3
, and the introduction of the first digit on the other end would simply be an addition. But there are complications, such as discarding the last "carry" (perhaps via amod
operation), and I couldn't get it to work with few bytes – Luis Mendo – 2015-08-14T23:29:50.160