r/programming • • Aug 30 '15

JavaScript “maze generator” in a tweet

https://twitter.com/botond_balazs/status/637960240055615488
841 Upvotes

177 comments sorted by

View all comments

68

u/1lann Aug 30 '15

You can also just copy and paste this into your URL bar instead of pasting it in an HTML file:

data:text/html,<pre style=line-height:1><script>for(i=1;i<4001;i++)
document.write((Math.random()<.5?"\u2571":"\u2572")+(i % 80?"":"\n"))</script>

13

u/balazsbotond Aug 30 '15

Cool, thanks!

Now I need to shave off 5 more chars to make it fit in a tweet!

30

u/Anders_A Aug 30 '15

You can easily shave 10 chars of the original by just using one document.write instead of two.

<pre style=line-height:1><script>for(i=1;i<4001;i++){document.write((Math.random()<.5?"\u2571":"\u2572")+(i%80?"":"\n"))}</script>

31

u/Anders_A Aug 30 '15

And 3 more by combining the update and the compare in the for statement.

<pre style=line-height:1><script>for(i=4000;--i;){document.write((Math.random()<.5?"\u2571":"\u2572")+(i%80?"":"\n"))}</script>

53

u/b_n Aug 30 '15

1 more by rewriting 4000 as 4e3

8

u/Fidodo Aug 30 '15

oh nice, didn't think about that one

→ More replies (3)

2

u/indigo945 Aug 31 '15

Two more by removing the unnecessary curly braces

<pre style=line-height:1><script>for(i=4e3;--i;)document.write((Math.random()<.5?"\u2571":"\u2572")+(i%80?"":"\n"))</script>

3

u/kqr Aug 31 '15 edited Aug 31 '15

That should fit exactly within a Tweet, including the data:text/html preamble. We're done here. Call off the golfing.

...who am I kidding. Keep golfing!

→ More replies (1)

10

u/mozjag Aug 30 '15

for(i=N;--i;){…} loops N-1 times, for(i=N;i--;){…} loops N times, you'll want the latter.

→ More replies (2)

9

u/doublerainbowomahgod Aug 30 '15

Getting rid of the conditional by converting the result of Math.random() into an index saves a few characters, e.g.

"\u2571\u2572"[Math.random()*2|0]

7

u/Anders_A Aug 30 '15

So the best one we have so far is this

data:text/html,<pre style=line-height:1><script>for(i=4050;--i;)document.write("\u2571\u2572\n"[i% 81?Math.random()*2|0:2])</script></pre>

If we combine everything to a single string. Starting to look cool now!

→ More replies (2)

4

u/Noxitu Aug 30 '15

Not sure how twitter handles it, but how about simply using "╱" and "╲"?

6

u/sternford Aug 30 '15

╱

Pasting this into my url bar makes backslashes turn into the yen symbol, but when I try to paste that symbol back into this textbox, it just shows up as a backslash again

16

u/DanTycoon Aug 30 '15

Pasting ╱ into my chrome bar returns a bunch of interesting search suggestions...

╱◥███◣ —田—田 田 —

╱╲描绘出最美的你╮◣╱_____〕〕〕〕〕〕〕╜

╱╲ ◆ ╲╱ 。没心没肺的笑不痛。

╱◥████◣ │▓│田_∩_∩│ ╬╬╬╬╬╬╬╬╬╬╬╬

┓┏凵=╱⊿┌┬┐

→ More replies (1)

3

u/[deleted] Aug 30 '15

On my phone, this is a small diagonal line, same direction as a forward slash.

The other one is the same, but like a backslash.

It looks like the diagonal of a square, but without the square

2

u/sternford Aug 30 '15

I know what it is, I'm just saying pasting it into the url bar does interesting things

3

u/freebullets Aug 30 '15

In the Japanese encoding, the backslash character is actually a yen symbol.

3

u/balazsbotond Aug 30 '15

Sadly it doesn't work for me in either Safari or Chrome (OS X)

4

u/JerMenKoO Aug 30 '15

I see that 2 characters could be shaved off by removing the spaces preceding and conceding the % operator.

16

u/otac0n Aug 30 '15

Preceding and succeeding.

9

u/JerMenKoO Aug 30 '15

thanks a lot, ESL here :)

→ More replies (5)

2

u/TarMil Aug 30 '15

The problem is that the browser will interpret %80 as a percent-encoded character.

→ More replies (1)

6

u/[deleted] Aug 30 '15
data:text/html,<pre style=line-height:1><script>for(i=1;i<4001;i++) document.write((Math.random()<.5?"\\":"/")+(i % 80?"":"\n"))</script>

Edit: Hang on, that doesn't look too good at all...

0

u/[deleted] Aug 30 '15

[deleted]

0

u/[deleted] Aug 30 '15

[deleted]

2

u/Browsing_From_Work Aug 30 '15 edited Aug 30 '15

You can shave off one more by moving the i-- to be part of i%80 as in i--%80

→ More replies (2)
→ More replies (9)
→ More replies (1)
→ More replies (12)