I'm creating a simple challenge here as a first test of the community's reactions. Please let me know if this challenge is the kind of thing you guys like or what I can improve... :)
The task is this: Render a chess board in Lua in smallest code possible.
Here is an example solution: http://tinybrain.de/145
As you can see, there is definitely room for getting smaller than 657 bytes (# of bytes are printed on the page). All the information you need for coding should be on the page or, hopefully, on tinybrain.de. Mailed questions totally welcome too. Lua API overview: http://tinybrain.de:8080/tb/debug.php?cmd=144
I am not demanding your chess board to have the exact same colors or even the same size. It should look like a chess board, that's all.
You need to register on TinyBrain.de to submit and test code. (Sorry, hope that is OK... It's free and I'm not a data peddler, so no harm there... :)
Edit: For testing your code locally (without any registration or Internet connection), you can download the TinyBrain runtime (magic.jar). Here is the link: http://tinybrain.de:8080/magic.jar
-
\$\begingroup\$ Does it have to be rendered as an image? Your question doesn't specify that. \$\endgroup\$Cel Skeggs– Cel Skeggs01/12/2014 21:38:39Commented Jan 12, 2014 at 21:38
-
\$\begingroup\$ Yes, that was the original idea... What's the alternative? Render it as ASCII? Well, why not. We can collect those solutions too. :) In that case, just use print() statement(s)! \$\endgroup\$Stefan Reich– Stefan Reich01/12/2014 21:52:09Commented Jan 12, 2014 at 21:52
-
\$\begingroup\$ Could you bring in the examples from tinybrain.de and make the source code for magic.jar available (I am a bit hesitant to run unkown binaries on my machine that I've downloaded from the internet). \$\endgroup\$user12166– user1216602/16/2016 19:54:59Commented Feb 16, 2016 at 19:54
-
\$\begingroup\$ Closing as unclear because tinybrain.de is now a dead link. \$\endgroup\$The Fifth Marshal– The Fifth Marshal11/27/2020 16:55:53Commented Nov 27, 2020 at 16:55
5 Answers 5
For a fancy ASCII art solution:
print("/"..("-"):rep(32).."\\")for y=0,15 do print("|"..(".... "):rep(5):sub(1+(y-y%2)*2%8):sub(1,32).."|")end print("\\"..("-"):rep(32).."/")
At 145 characters.
For a small ASCII art solution:
for i=1,8 do print(("# "):rep(9):sub(i,i+7))end
At 47 characters.
-
\$\begingroup\$ Great job :) Is it the shortest ASCII version? Using "rep" is pretty smart! \$\endgroup\$Stefan Reich– Stefan Reich01/12/2014 22:26:14Commented Jan 12, 2014 at 22:26
-
\$\begingroup\$ (Typo in the second solution gives a ninth row.) \$\endgroup\$breadbox– breadbox01/12/2014 22:42:18Commented Jan 12, 2014 at 22:42
-
\$\begingroup\$ @Stefan I'm unsure if it's the shortest solution, but it's probably close. \$\endgroup\$Cel Skeggs– Cel Skeggs01/12/2014 22:44:50Commented Jan 12, 2014 at 22:44
-
\$\begingroup\$ @breadbox fixed. \$\endgroup\$Cel Skeggs– Cel Skeggs01/12/2014 22:48:07Commented Jan 12, 2014 at 22:48
Starting with col6y's "small ASCII" solution (47):
for i=1,8 do print(("# "):rep(9):sub(i,i+7))end
sometimes brute force is better (43):
for i=1,4 do print("# # # # \n # # # #")end
:)
-
1\$\begingroup\$ You don't really need the trailing space after the newline, and Lua has sugar for calling functions with a string literal:
print"# # # #\n # # # #"
. Though, you could save yet more chars anyway by usingstring.rep
instead:print(("# # # #\n # # # #\n"):rep(4))
clocks in at 37. \$\endgroup\$FireFly– FireFly01/17/2014 12:02:11Commented Jan 17, 2014 at 12:02 -
\$\begingroup\$ Nice. Wish I'd noticed that. \$\endgroup\$Cel Skeggs– Cel Skeggs05/09/2015 20:21:45Commented May 9, 2015 at 20:21
38
Combining 14921's and col6y's ideas:
print(("# # # # \n # # # #\n"):rep(4))
83 characters
This snippet takes advantage of the description's offer to omit one of the dimensions, and to use a single integer to represent the pixel color.
w=256
p={}
for i=0,65535 do p[i+1]=5595493-((i-i%32)/32+(i-i%8192)/8192)%2*26214 end
(The character count doesn't include the newline on line 2, which I've included solely for legibility.)
The logic is pretty straightforward. The expression (i-i%32)/32
is equivalent to math.floor(i/32)
-- in other words, integer division. Written in C's bitwise operators (which Lua lacks), the logic would look like this:
p[i] = (((i >> 5) ^ (i >> 13)) & 1) ? dark : light
This solution should produce identical output as the example given in the description.
You can shave off several characters by changing the colors to pure black and white, assuming -1
can be used to represent the white color:
w=256
p={}
for i=0,65535 do p[i+1]=((i-i%32)/32+(i-i%8192)/8192)%2-1 end
If I also change the size to be 64x64 (which the description permits), I can bring it down to 65 characters:
w=64
p={}
for i=0,4095 do p[i+1]=((i-i%8)/8+(i-i%512)/512)%2-1 end
However, the description doesn't seem to have any objective limits as to the chessboard's appearance. (Rookie mistake.) Let's go ahead and use the color 0x000000
for the white squares, and 0x000001
for the dark squares. And let's have the chessboard squares be 1x1. I can then bring it down to 48 characters:
w=8
p={}
for i=0,63 do p[i+1]=(i+(i-i%8)/8)%2 end
Or, if we forgo graphics entirely and stick to ASCII, we can use this 54-character solution and still be legible:
for i=1,8 do print(('* * * * *'):sub(1+i%2,8+i%2))end
-
\$\begingroup\$ Oh! I see we have someone with some good knowledge of Lua. Really nice. It's a very good job I'd say. \$\endgroup\$Stefan Reich– Stefan Reich01/12/2014 22:17:43Commented Jan 12, 2014 at 22:17
-
\$\begingroup\$ I do see an opportunity for improvement. 2 bytes can probably be saved both on the graphics version and on the text version. With the text version, I'm sure it's possible, my idea for graphics version would have to be tested. \$\endgroup\$Stefan Reich– Stefan Reich01/12/2014 22:20:10Commented Jan 12, 2014 at 22:20
-
\$\begingroup\$ Oh, and another thing: -1 works as white in the pixel array. A white that is good as invisible is REALLY stretching it a bit... ^^ \$\endgroup\$Stefan Reich– Stefan Reich01/12/2014 22:22:29Commented Jan 12, 2014 at 22:22
-
\$\begingroup\$ @StefanReich -1 for white you say? I'll change that at once. \$\endgroup\$breadbox– breadbox01/12/2014 22:27:30Commented Jan 12, 2014 at 22:27
27-29 letters :-þ supersmall diagonal but not ascii ..
print(("♦♦♦♦♦♦♦♦\n"):rep(5));
3D version 35 letters :)
print(("◄►◄►◄►◄►◄►◄►◄►\n"):rep(8));
extra nice version :)
print(("▇ ▇ ▇ ▇\n ▇ ▇ ▇ ▇\n"):rep(4));