# Encoding an URL in brainfuck

Inspired by this question, your goal today is to encode an URL in brainfuck.

My browser has been hacked! The location bar has been turned into a brainfuck interpreter. I need a program that turns an URL into a brainfuck program. My computer is also very slow, so if the brainfuck program is small it's better.

Oh and I only have one working tab left open, my twitter. So you can only send me the program in a tweet.

1. Write a program that fits in a tweet (140 characters) in any language that takes a string as input and outputs a brainfuck program.

2. This brainfuck program will be run without input and will generate an output.

3. This output will be inserted into the location bar of Google Chrome, and executed.

4. If the behaviour of Google Chrome is identical to the behaviour you would get by entering the input string from #1, then the solution is valid. Redirection by URL shorteners is considered different behaviour - stay at least on the same domain.

You may assume the brainfuck interpreter has infinite precision signed integers in each cell and an unlimited amount of cells.

Your score is determined by the sum of the lengths of the brainfuck programs generated to encode the following set of URLs:

https://www.google.com/
https://codegolf.stackexchange.com/questions/47895/
http://www.golfscript.com/golfscript/examples.html
http://en.wikipedia.org/wiki/Percent-encoding
ftp://ftp.freebsd.org/pub/FreeBSD/
https://github.com/isaacg1/pyth/blob/master/doc.txt#L92


Smallest score wins.

• May we assume that the input would be formatted like this: http://www.google.com/ or will it ever simply be google.com? (Both are valid as far as Google Chrome is concerned, but your list only contains the full forms) P.S. I almost have a desire to write an extension for chrome that does this... – BrainSteel Mar 19 '15 at 13:51
• @BrainSteel The input for your encoder will be exactly as stated as in the question (each line is one input). The output of the brainfuck may be any format accepted by Google Chrome, as long as it has the same behaviour as the original input string. So if your program takes input http://www.google.com/ the brainfuck program it produces may output google.com, www.google.com, etc, because they all result in the same page. But for example ftp://ftp.freebsd.org/pub/FreeBSD/ is not the same as ftp.freebsd.org/pub/FreeBSD/. – orlp Mar 19 '15 at 13:59
• Ah, thank you for clarifying! Sorry, I'm a bit slow this morning. – BrainSteel Mar 19 '15 at 14:09
• I would like to clarify that I failed, and that ftp://ftp.freebsd.org/pub/FreeBSD/ is indeed the same as ftp.freebsd.org/pub/FreeBSD/ in Google Chrome. This makes the challenge a bit easier than I intended, but such is life. I won't change the requirements, so it's safe to just strip off anything before ://. – orlp Mar 19 '15 at 21:42

# Pyth - 118 bytes, score: 154 + 567 + 597 + 620 + 530 + 875 + 1092 = 663066234653 4435

Now using the factor multiplication algo!

This is a pretty simple algorithm, although slightly better than the above ones. I was planning to use Pyth, but with the current algorithm Python fits<140 bytes.

=zecz"//"=z?>z4}"www."zz=z?Pzqez\/zFNzKCNJ/^K.5 1=d\+pk+holN[?*-KZd>KZ*-ZK\-+\>*dK+++++\>*Jd"[>"*Jd"<-]>"*-K*JJd)\.=ZK


It first strips out the http:// by using split("//") and the www. and / at the end. Then it uses the min function to check which is the best of the the three options that is considered:

1. Use + or - to update current cell value to desired cell value
2. Go to new cell and just fill with +'s
3. Go to new cell and use multiplication to update by doing sqrt(n)*sqrt(n)+remainder since square has minimum perimeter of constant area. http://cydathria.com/bf/bf_ex3.html

I then always put a . to output each char.

A detailed explanation of the new code is coming soon. I kind of half-assed the golfing, especially the stripping of www. and the / at the end but I can't be bothered since I'm 20 bytes under the limit.

This surprisingly works on the ftp:// urls since this is chrome and chrome automatically makes ftp.freebsd.org/pub/FreeBSD/ into ftp://ftp.freebsd.org/pub/FreeBSD/

I plan to next use a dictionary to store the cell values and see if the character was already made in a cell.

## Brainfuck Omnibox Extension

That's right! After I saw @BrainSteel's comment, I knew I had to make it. This doesn't take over the entire omnibox, only when you do bf,tab. Also since the omnibox api is weird, it doesn't let me forward the code to the omnibox, I have to handle it myself. So I do my own http:// checking and then set the tab's url.

Since I didn't feel like paying the \$5 necessary to put your extension on the webstore, you guys will have to install it manually. Just download the zip from here: https://drive.google.com/file/d/0B0qkKIzGc0QiNzBzVUJjZkRIZ0k/view?usp=sharing, unzip, go into the extensions page in chrome, click dev mode option box on top-right, and load the unpacked extension.

Hope you guys enjoy! :)

• This won't work on the ftp:// URL. – PurkkaKoodari Mar 19 '15 at 20:59
• @Pietu1998 that's what I thought at first but Chrome surprised me. Try putting in ftp.freebsd.org/pub/FreeBSD/ in your address bar and Chrome will make it ftp:// ftp.freebsd.org/pub/FreeBSD/ – Maltysen Mar 19 '15 at 21:01
• @Maltysen Oops. I had intentionally included that URL to prevent this optimization, but it looks like I failed. This URL is indeed different, but it seems Chrome is indeed smart enough to recognize and add ftp://. Oh well. – orlp Mar 19 '15 at 21:41

# 263 + 816 + 911 + 955 + 827 + 1355 + 1779 = 7269 6906 bytes

Update: 6 bytes less, thanks to nutki!

Update again, 128 bytes. Code is significantly less readable. It now attempts to strip the URL of ftp://, too.

Update the third, at 139 bytes. Code is a bit more readable, and got rid of some nasty undefined behavior. Now, if the site name is preceded by www., it is stripped. I got under 7k, but I think I must concede to Maltysen with Pyth. Well done.

#define p putchar
a;main(int c,char**V){while(*V[1]++-47);*++V+=1;for(*V+=**V-'w'?0:4;c=**V;*V+=1,p(46))for(;c-a;a+=c>a?1:-1)p(c>a?43:45);}


Unraveled a bit:

#define p putchar
a;
main(int c,char**V){
while(*V[1]++-47);
*++V+=1;
for(*V+=**V-'w'?0:4;c=**V;*V+=1,p(46))
for(;c-a;a+=c>a?1:-1)
p(c>a?43:45);
}


This code takes a URL at the command line, and converts it into BF using only .,-, and +. The code makes an attempt to strip the string of ftp://,http://,https://, and www. before converting to BF. Here is a list of outputs of the outputs:

google.com/
codegolf.stackexchange.com/questions/47895/
golfscript.com/golfscript/examples.html
en.wikipedia.org/wiki/Percent-encoding
ftp.freebsd.org/pub/FreeBSD/
github.com/isaacg1/pyth/blob/master/doc.txt#L92

• How about using one inner loop: for(;c-a;a+=c>a,a-=c<a)p(c>a?43:45) should work. – nutki Mar 19 '15 at 16:29
• @nutki Impressive! Thank you! – BrainSteel Mar 19 '15 at 16:37
• "Note that if we may assume no URL begins with anything other than http://, https://, or ftp://, we can drop 10 bytes from this solution." Your solution only has to work for the dataset above, so you may assume whatever you want, as long as the dataset will still be handled correctly/ – orlp Mar 19 '15 at 21:44
• @orlp Hooray! That sounds like 12 bytes to play with! Thank you for this challenge, it's been great fun! – BrainSteel Mar 19 '15 at 21:48

# Brainfuck, 77 bytes, Score = 31647

Score is the sum of codepoint+4 for every character.

+++++[>+++++++++<-]>+[>++>+>++>+>+<<<<<-]>+>->->>--->,[[<.>-]<<.<.<.<.>>>>>,]


(Uses BF implementation with EOF = 0.)

Example:

input:
!bar

output:
+++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]


# JavaScript (ES6) - 137 bytes, score - 7413

This uses only ., + and -. You can test it in Firefox console.

B=s=>[...s.replace(/^h.+\/\//,'')].map((c,i,a)=>(v=i?c[C]()-a[i-1][C]():c[C='charCodeAt']())?'-+'[v>0|0].repeat(v>0?v:-v):'').join('.')+'.'


This is my score counter:

[