Hodorize a String
Hodor, 602 582 bytes
HoDoRHoDoR HODOR! (Hodor ){r='';Hodor!? =Hodor .Hodor!? hodor? HODOR? hodor! hodor. h;HODOR{}(Hodor? =0;Hodor? <Hodor!? ;++Hodor? ){hodor =Hodor .hodor hHODOR rHodor... odhodor? hodor?!? hodor. (Hodor? );HODOR? =(0|hodor /4+16)%32;r+='H';HODOR =HHHOOODDDDOOORRR!!! hodor?!? rrHODOR HODOR!?! (0|HODOR? /2+2);HOdor!!!(HODOR? %2)HODOR [0|(HODOR? +1)/4]=',';r+=HODOR? ==1?'ODOR':HODOR .HODOR?! oHodor? HODOR? (' hodor').Hodor Hodor!? Hodor? hodor hodor? (2);r+=['. ',Hodor? ==Hodor!? -1?'....':'.\HODOR? \HODOR? ','? ','! '][hodor %4]}HODOR:: r.Hodor Hodor!? Hodor? hodor hodor? (0,-1)}
Here is an ungolfed version... you know... for readability:
HoDoRHoDoR HODOR! (Hodor ){
r='';
Hodor!? =Hodor .Hodor!? hodor? HODOR? hodor! hodor. h;
HODOR{}(Hodor? =0;Hodor? <Hodor!? ;++Hodor? ){
hodor =Hodor .hodor hHODOR rHodor... odhodor? hodor?!? hodor. (Hodor? );
HODOR? =(0|hodor /4+16)%32;
r+='H';
HODOR =HHHOOODDDDOOORRR!!! hodor?!? rrHODOR HODOR!?! (0|HODOR? /2+2);
HOdor!!!(HODOR? %2)HODOR [0|(HODOR? +1)/4]=',';
r+=HODOR? ==1?'ODOR':HODOR .HODOR?! oHodor? HODOR? (' hodor').Hodor Hodor!? Hodor? hodor hodor? (2);
r+=['. ',Hodor? ==Hodor!? -1?'....':'.\HODOR? \HODOR? ','? ','! '][hodor %4]
}
HODOR:: r.Hodor Hodor!? Hodor? hodor hodor? (0,-1)
}
Yep, there is a language called Hodor. (And it's rather new.) It's essentially just a string substitution for JavaScript. The corresponding JS looks like this:
function f(s){
r='';
l=s.length;
for(i=0;i<l;++i){
c=s.charCodeAt(i);
n=(0|c/4+16)%32;
r+='H';
a=new Array(0|n/2+2);
if(n%2)a[0|(n+1)/4]=',';
r+=n==1?'ODOR':a.join(' hodor').slice(2);
r+=['. ',i==l-1?'....':'.\n\n','? ','! '][c%4]
}
return r.slice(0,-1)
}
Hence, my submission is a function taking a string and returning the Hodorised equivalent.
Unfortunately, there doesn't seem to be any information about how Hodor actually works (apart from the source code of the interpreter), but it comes with a JS2HD translation script.
Note that Hodor's substitutions are also applied within string literals. So while you may just call the above function as
HODOR! ("CEE")
You can also use the following:
HODOR! ("Hodor... HooodorrHodor HooodorrHodor ")
where Hodor... HooodorrHodor HooodorrHodor
will be substituted with CEE
. Note that this means, that if your string itself contains certain forms of Hodor
you'll have to encode it, or else it will be substituted. E.g. "Hodor. "
is actually "m"
in JavaScript. The catch is that the letters [dhor]
(case insensitively) are not replaced at all, so you can't even encode this. One solution is to split up the literal, like "Hod"+"or. "
, such that the substitution step doesn't find anything to decode.
Hodor! Hodor hodor? Hodor hodor hodor hodor hodor.
Hodor hodor hodor, hodor hodor hodor.
Hodor hodor hodor hodor hodor hodor hodor, hodor hodor hodor hodor hodor hodor hodor. Hodor hodor hodor hodor hodor hodor hodor hodor hodor hodor hodor hodor hodor. Hodor hodor hodor hodor hodor hodor hodor hodor hodor hodor hodor hodor hodor hodor hodor hodor. Hodor hodor hodor hodor hodor hodor hodor, hodor hodor hodor hodor hodor hodor hodor hodor.
Hodor hodor hodor hodor hodor hodor hodor hodor hodor hodor hodor hodor hodor. Hodor hodor hodor hodor hodor? Hodor hodor hodor hodor hodor hodor hodor hodor.
Hodor hodor hodor, hodor hodor hodor hodor. Hodor hodor, hodor hodor hodor.
Hodor hodor hodor hodor hodor hodor hodor!
That's just the language and size header!? Nevermind then, let's just encode the post in boring old ASCII. Sorry if you have trouble understanding the post, Hodor.
CJam, 85 bytes
Here's my attempt. Some parts of this code felt rather clunky when I wrote them, so there's likely potential to beat this!
qW%{'@^4md('.X1$N?_++a"?!."XLS?f++=\(_)2md","*1$)"hodor":Ha*H@+@(2/\tS*0:X'HtHeu?}%W%
Try it online.
Explanation
qW% "Read and reverse the input.";
{ "Map each character c:";
'@^4md "Calculate (c^64)/4, which is the sentence content determiner,
and c%4, which is the sentence ending determiner.";
( "Calculate c%4-1 for later.";
'.X1$N?_++ "Produce the sentence ending string for the case that c%4==1,
which is '.' followed by two copies of:
- '.' if this is the first processed (last actual) character
- or a newline otherwise.
a"?!."XLS?f "Produce a list of the sentence ending strings for the cases
that c%4 is 2, 3, or 0, which are:
- '?', '!', and '.' if this is the first processed character
- or these punctuation marks each followed by a space
otherwise.";
++=\ "Prepend the sentence ending string for the case that c%4==1
to the list for the other cases and select the element at the
index (c%4-1)%4 as the correct sentence ending string.";
( "Calculate (c^64)/4-1 for later.";
_)2md "Calculate the quotient and remainder of the sentence content
content determiner divided by 2.";
","* "Produce a string of commas with a length of the remainder
calculated above (either 0 or 1).";
1$)"hodor":Ha* "Produce a list of copies of 'hodor' with a length of the
quotient calculated above plus one.";
H@+@(2/\t "Append the string of commas (either empty or one comma) to
the middle 'hodor'.";
S* "Join the sentence words into a string a space between each.";
0:X "Signal that later characters are not the first processed.";
'Ht "Capitalize the first letter of the sentence (always 'h').";
Heu "Produce the string 'HODOR'.";
? "Select the correct sentence content, which is 'HODOR' if
(c^64)/4-1 is zero or the full sentence produced before that
otherwise.";
}%
W% "Reverse the reversed translation to obtain the forward one.";
"Implicitly print the result.";
Python 2, 219 198 bytes
Try it here
Uses the same method as Martin's JavaScript, and it's several bytes shorter in Python. I make use of Python 2.7's handy integer division. Thanks to Sp3000 for some golfing.
def f(s,r='',i=2):
for j in s:c=ord(j);n=(c/4+16)%32;a=['']*(n/2+2);a[-~n/4]=','[:n%2];r+='H'+[' hodor'.join(a)[2:],'ODOR'][n==1]+['. ',['.\n\n',4*'.'][i>len(s)],'? ','! '][c%4];i+=1
return r[:-1]
And for your own entertainment and my own enjoyment and curiosity...
Here it is as a lambda function!!!
Python 2, 349
Try it here, along with an older version of the program above.
f=lambda s:'H'.join(['']+[('ODOR'if(ord(s[i])/4+16)%32==1 else' hodor'.join((['']*(((ord(s[i])/4+16)%32)/2+2))[:((ord(s[i])/4+16)%32+1)/4]+([',']if ord(s[i])/4%2 else[''])+(['']*(((ord(s[i])/4+16)%32)/2+2))[((ord(s[i])/4+16)%32+1)/4+1*(ord(s[i])/4%2<1):])[2:])+['. ',4*'.'if i+2>len(s)else'.\n\n','? ','! '][ord(s[i])%4]for i in range(len(s))])[:-1]
Creating this involved deeper and deeper substitutions as well as some extra creativity.
- Every
j
is replaced withs[i]
. - Every
c
is replaced withord(s[i])
. Every
n
is replaced with(ord(s[i])/4+16)%32
.- Except where I only need
n%2
, so I use(ord(s[i])/4%2
instead.
- Except where I only need
Finally,
a
is replaced with(['']*(((ord(s[i])/4+16)%32)/2+2))[:((ord(s[i])/4+16)%32+1)/4]+([',']if ord(s[i])/4%2 else[''])+(['']*(((ord(s[i])/4+16)%32)/2+2))[((ord(s[i])/4+16)%32+1)/4+1*(ord(s[i])/4%2<1):]
.- This is because we cannot assign a value, so we must split the list in half, conditionally append the comma, then append the last half again.