Jump to content




Lua Code Golf


114 replies to this topic

#1 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 16 August 2013 - 07:34 PM

A change of rules - you are now allowed to use languages other than Lua. Have fun!

I feel like it's time that this particular forum had an actually educational (but fun!) game, and as such I would like to present to everyone the concept of code golf. In a sentence, code golf is:

Wikipedia said:

Code Golf is a type of recreational computer programming competition in which participants strive to achieve the shortest possible code that implements a certain algorithm.

In other words, whoever manages to write the shortest code will win the round! A few ground rules:
  • "Short code" is determined by the number of characters not including whitespace (you can find a good character counter here!
  • Your code must be functional, but readability is of course not necessary (kudos if you can have your code be both short and readable though)
  • You do not necessarily need to subscribe to "good practice". Assume that your code will always be used correctly.
  • Challenges will run infinitely or until it is agreed that the code cannot be shortened any further.
  • Anyone can suggest a challenge, but see below for the guidelines that a challenge must subscribe to
  • Stick to the spirit of the challenge!
  • And most importantly, have fun and don't be ashamed of posting code that is longer than other's. Code golf is difficult, and it takes both time and practice to perfect the art. The best way to learn is by trying again and again even if you don't win each time. So give it your best shot!
Challenge guidelines:
  • Challenges must be at least somewhat challenging. For example, an example of a bad challenge would be "make a program that prints hello world". There is a point where you literally cannot shorten code at all. An example of a good challenge would be "Write a button API that supports mouse click checking and custom colors".
  • Challenges may have strictures. For example, you can say "Write a program that finds lua comments in text without using any string functions other than string.find".
  • Any number of challenges can run at the same time! Present your challenge, and I'll add it to my main post ASAP as long as it subscribes to these guidelines.
  • As of right now, I want to try and keep challenges Lua based. If we find it difficult to shorten code much, I will allow more languages. However, Lua based challenges allows everyone to participate and not just those who have a degree in computer science or a lot of time to learn new languages on their handsopen
  • I've decided to open up challenges to new languages. This will, of course, not apply to those challenges which have already been issued specific to ComputerCraft and you may specify that a challenge should be language specific.
  • Challenges can and will be run in Minecraft on the specified type of Computer (Turtle challenges are allowed!).
  • If you start a challenge, please do your best to keep track of current best "score". This isn't necessarily required, but it would certainly help me out.
How to change a guideline: If more than three (3) people up-vote a suggestion to change the response, then I'll change said suggestion. Please don't reply to it, just upvote. That way I can keep the thread relatively clean

Current Challenges:
Spoiler

Good luck!

Edited by Bubba, 15 November 2013 - 11:04 AM.


#2 Sora Firestorm

  • Members
  • 89 posts

Posted 16 August 2013 - 08:01 PM

Code golf! I'm totally doing this. Sounds like a ton of fun. :D

#3 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 16 August 2013 - 08:02 PM

I agree with Sora... I like the sound of this... To Sublime!

EDIT:
Ok done... Submission for Bubba's Challenge (#1)
Spoiler

I'd also like to submit a challenge.
Spoiler

EDIT 2: Just fixing some things with the challenge

Edited by theoriginalbit, 16 August 2013 - 08:45 PM.


#4 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 16 August 2013 - 09:04 PM

View Posttheoriginalbit, on 16 August 2013 - 08:02 PM, said:

-snip-

Nicely done! theoriginalbit takes the lead. I've also added your suggestion to the OP.

Get crackin' people :)

#5 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 16 August 2013 - 09:09 PM

View PostBubba, on 16 August 2013 - 09:04 PM, said:

Nicely done! theoriginalbit takes the lead. I've also added your suggestion to the OP.
Thanks :)
Do I need to follow up with anyone that makes submissions for my challenge?

#6 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 16 August 2013 - 09:11 PM

View Posttheoriginalbit, on 16 August 2013 - 09:09 PM, said:

Thanks :)
Do I need to follow up with anyone that makes submissions for my challenge?

That might be a good idea. I'll keep track of them to the best of my ability, but it could get to be rather much if we have a lot of suggestions going at a time. I'll go ahead and that to the OP as well.

#7 Sora Firestorm

  • Members
  • 89 posts

Posted 16 August 2013 - 09:27 PM

Alright, here's mine. Compared to Bit, I've sacrificed a little bit of space for some pretty-ness :)

Spoiler

My API is very "set and forget", once you have your buttons initialized, all you have to do is loop the getClick() function. Very lazy-friendly :)

I'm interested to see future competitions!
~Sora

#8 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 16 August 2013 - 09:35 PM

Challenge: Golf a quine.

#9 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 16 August 2013 - 09:48 PM

Well it looks like I'm about to take the lead with the button challenge ;)/>
Character count: 255
Spoiler

Test program:
Spoiler

Extremely ugly and I'd never want to use it, but hey this is code golf :)/>

View PostLyqyd, on 16 August 2013 - 09:35 PM, said:

Challenge: Golf a quine.

Added.

View PostSora the Hedgehog, on 16 August 2013 - 09:27 PM, said:

-snip-

You should add your character count because otherwise it's not code golf :) The website I provided says it's 894 chars long.

#10 Sxw

  • Members
  • 306 posts
  • LocationWhenever, Wherever!

Posted 16 August 2013 - 09:57 PM

Quine: 46 chars
Spoiler
That... was easy?

Edited by Sxw, 16 August 2013 - 09:59 PM.


#11 ElvishJerricco

  • Members
  • 803 posts

Posted 16 August 2013 - 09:59 PM

View PostSxw, on 16 August 2013 - 09:57 PM, said:

Quine:
Spoiler
That... was easy?

First of all, it's considered cheating by most quine competition's standards to use the file system. Second of all, a quine outputs to the terminal it's source, it does not put it in a new file.

#12 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 16 August 2013 - 10:00 PM

View PostSxw, on 16 August 2013 - 09:57 PM, said:

Quine: 46 chars
Spoiler
That... was easy?

quine:1: Expected string, string

Shouldn't need arguments to run it, it's a quine. Also, use of the file system is cheating. :P

#13 ElvishJerricco

  • Members
  • 803 posts

Posted 16 August 2013 - 10:01 PM

Anyway, I have a challenge I'd like to submit: Top level coroutine override.

I already have a pretty good sumbission too ;)

Spoiler


#14 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 16 August 2013 - 10:10 PM

View PostLyqyd, on 16 August 2013 - 10:00 PM, said:

-snip-
Nice xD Not sure if it's actually possible to beat that, but then again maybe it is.

View PostElvishJerricco, on 16 August 2013 - 10:01 PM, said:

-snip-

Added. I must say that I love your creative thinking there :)

#15 ElvishJerricco

  • Members
  • 803 posts

Posted 16 August 2013 - 10:14 PM

View PostBubba, on 16 August 2013 - 10:10 PM, said:

Added. I must say that I love your creative thinking there :)/>

Thanks =) Just FYI, I had made a small mistake in re-writing my code here. I've corrected it and it's actually 100 characters, not 97

#16 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 16 August 2013 - 10:20 PM

For clarity, that was the error code produced by running the code I quoted, not an actual quine challenge submission. I'm saying this because Bubba's response to my quoted post seems to suggest that it was received as a submission. Running the string in my post results in a bios-line-then-quine-line name expected error.

#17 ElvishJerricco

  • Members
  • 803 posts

Posted 16 August 2013 - 10:20 PM

Oh I have a submission for a quine!



;) No code in the source means no output. The output matches the source soo... Quine! Hehehe...

#18 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 16 August 2013 - 10:22 PM

View PostElvishJerricco, on 16 August 2013 - 10:20 PM, said:

Oh I have a submission for a quine!



;) No code in the source means no output. The output matches the source soo... Quine! Hehehe...

Probably technically against the rules ;)

#19 Sorroko

  • Members
  • 87 posts

Posted 16 August 2013 - 10:23 PM

Dammit.
When I started coding the lead was 536 chars...
Well I'll post my code anyways ;)

My entry for submission #1, the button api
430 chars without whitespace
Spoiler

My test code
Spoiler


#20 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 16 August 2013 - 10:24 PM

View PostLyqyd, on 16 August 2013 - 10:20 PM, said:

For clarity, that was the error code produced by running the code I quoted, not an actual quine challenge submission. I'm saying this because Bubba's response to my quoted post seems to suggest that it was received as a submission. Running the string in my post results in a bios-line-then-quine-line name expected error.

Oops! I should know better by now what error would be produced. Ah well, the concept would still probably win so I'll leave you as the originator of the idea and just change the character count.
The new code would be:
bios:339: [string "a"]:1: '<name>' expected

Its length is 39 chars long.

Edit: I've removed this as the top result because it's not a "true" quine. 44 chars is now the length to beat!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users