11
\$\begingroup\$

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
http://ftp.gnu.org/non-gnu/nethack/nethack-3.4.0.tar.gz.back-RSN.README

Smallest score wins.

\$\endgroup\$
  • \$\begingroup\$ 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... \$\endgroup\$ – BrainSteel Mar 19 '15 at 13:51
  • 2
    \$\begingroup\$ @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/. \$\endgroup\$ – orlp Mar 19 '15 at 13:59
  • \$\begingroup\$ Ah, thank you for clarifying! Sorry, I'm a bit slow this morning. \$\endgroup\$ – BrainSteel Mar 19 '15 at 14:09
  • \$\begingroup\$ 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 ://. \$\endgroup\$ – orlp Mar 19 '15 at 21:42
7
\$\begingroup\$

Pyth - 118 bytes, score: 154 + 567 + 597 + 620 + 530 + 875 + 1092 = 6630 6623 4653 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! :)

\$\endgroup\$
  • \$\begingroup\$ This won't work on the ftp:// URL. \$\endgroup\$ – PurkkaKoodari Mar 19 '15 at 20:59
  • 1
    \$\begingroup\$ @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/ \$\endgroup\$ – Maltysen Mar 19 '15 at 21:01
  • \$\begingroup\$ @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. \$\endgroup\$ – orlp Mar 19 '15 at 21:41
4
\$\begingroup\$

C, 140 134 132 138 128 139 Bytes

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
ftp.gnu.org/non-gnu/nethack/nethack-3.4.0.tar.gz.back-RSN.README
\$\endgroup\$
  • \$\begingroup\$ How about using one inner loop: for(;c-a;a+=c>a,a-=c<a)p(c>a?43:45) should work. \$\endgroup\$ – nutki Mar 19 '15 at 16:29
  • \$\begingroup\$ @nutki Impressive! Thank you! \$\endgroup\$ – BrainSteel Mar 19 '15 at 16:37
  • \$\begingroup\$ "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/ \$\endgroup\$ – orlp Mar 19 '15 at 21:44
  • \$\begingroup\$ @orlp Hooray! That sounds like 12 bytes to play with! Thank you for this challenge, it's been great fun! \$\endgroup\$ – BrainSteel Mar 19 '15 at 21:48
4
\$\begingroup\$

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:
+++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.[-]
\$\endgroup\$
0
\$\begingroup\$

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:

[
    'https://www.google.com/',
    'http://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',
    'http://ftp.gnu.org/non-gnu/nethack/nethack-3.4.0.tar.gz.back-RSN.README'
].map(B).join('').length
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.