Jump to content




Lua Code Golf


114 replies to this topic

#21 immibis

    Lua God

  • Members
  • 1,033 posts
  • LocationWellington, New Zealand

Posted 16 August 2013 - 10:49 PM

Lua quine, 44 chars:
q="q=%qprint(q:format(q))"print(q:format(q))


#22 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 16 August 2013 - 10:55 PM

And there's a true quine that doesn't take advantage of the structure of CC's error messages!

#23 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 16 August 2013 - 11:02 PM

View Postimmibis, on 16 August 2013 - 10:49 PM, said:

Lua quine, 44 chars:
q="q=%qprint(q:format(q))"print(q:format(q))

And Immibis takes the #1 spot for having a valid quine :)

#24 lieudusty

  • Members
  • 419 posts

Posted 16 August 2013 - 11:09 PM

Challenge Request: A program that packages a folder into a file and unpackages it

Guidelines:
  • The program must be in the form of a function (ex: package(pathToFolder) )

Grim Reaper leading with 233 chars

#25 Grim Reaper

  • Members
  • 503 posts
  • LocationSeattle, WA

Posted 16 August 2013 - 11:14 PM

EDIT: Removed for not understanding the requirements :P

#26 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 16 August 2013 - 11:19 PM

For the button challenge, I wanted to go for something that was rather functional and worked pretty well. It was good up until I saw bubba's challenge, and I had to make this look a little ugly to beat the character count. Nonetheless, it still works exactly as I want, and in a rather elegant manner. Nonspace character count: 262.

Spoiler

The usage is creating your own table, which these functions read and draw with accordingly. The love2d library bump.lua works on the same principle.

Example (changes the button to a random color on click):

os.loadAPI('button')

local myButton = {
  x = 3;
  y = 2;
  text = "This is a button!";
  bgColor = colors.blue;
  textColor = colors.lightBlue;
}

while true do
  button.draw(myButton)
  local _, _, mx, my = os.pullEvent('mouse_click')
  if button.clicked(myButton, mx, my) then
    button.bgColor = 2 ^ math.random(14)
    button.textColor = 2 ^ math.random(14)
  end
end


#27 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 16 August 2013 - 11:26 PM

View Postlieudusty, on 16 August 2013 - 11:09 PM, said:

Challenge Request: A program that packages a folder into a file and unpackages it

Guidelines:
  • The program must be in the form of a function (ex: package(pathToFolder) )

Added :)

View PostKingdaro, on 16 August 2013 - 11:19 PM, said:

-snip-

Heh. You've kept yours both functional and clean. I like it :) Unfortunately for you, I removed a bit of unnecessary code and am now in the lead again with 255 chars, though I don't think it would be difficult for you to catch back up.

#28 Grim Reaper

  • Members
  • 503 posts
  • LocationSeattle, WA

Posted 16 August 2013 - 11:27 PM

Here's my directory packer entry:

233 Characters without white space.
Spoiler

EDIT: The path to folder should be the first argument to the program.

#29 immibis

    Lua God

  • Members
  • 1,033 posts
  • LocationWellington, New Zealand

Posted 16 August 2013 - 11:32 PM

View PostGrim Reaper, on 16 August 2013 - 11:14 PM, said:

If we can take advantage of the error structure in CC, here's my quine for 40 characters:

error(("error(%q)"):format("error(%q)"),0)

That doesn't even work, it just prints this:
error("error(%q)")

I think the button API needs clearer requirements - e.g. can the buttons be arbitrary width (Bubba's is always 4 + text length)?
Are exported function names allowed to be shortened to save space? (buttons.draw -> buttons.d etc)
Maybe you could specify the public interface of the API (e.g.
function buttons.new(x, y, width, height, text, fgCol, bgCol) -> buttonObject
function buttons.draw(buttonObject)
function buttons.check(buttonObject, x, y)

Edit: Here's my button API that allows for arbitrary widths. 281 non-whitespace chars.
t=term
local x=function(n)return(" "):rep(n)end
new=function(B)B.s=(B.w-B.t:len())/2 B.t=x(math.floor(B.s))..B.t..x(math.ceil(B.s))end
rnd=function(B)t.setCursorPos(B.x,B.y)t.setTextColor(B.f)t.setBackgroundColor(B.b)t.write(B.t)end
chk=function(B,x,y)return y==B.y and x>=B.x and x<B.x+B.w end

Test program:
term.clear()

exitButton = {x=3, y=4, w=19, t="Exit", f=colors.white, b=colors.red}
clearButton = {x=3, y=5, w=8, t="Clear", f=colors.blue, b=colors.green}
new(exitButton)
new(clearButton)
rnd(exitButton)
rnd(clearButton)

while true do
  local evt,mb,x,y = os.pullEvent("mouse_click")
  if chk(exitButton, x, y) then break end
  if chk(clearButton, x, y) then
    term.setBackgroundColor(colors.black)
    term.clear()
    sleep(2)
    rnd(exitButton)
    rnd(clearButton)
  end
end


#30 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 16 August 2013 - 11:41 PM

Well, if we wanted to get ridiculously pedantic about the wording of the button API challenge, this would be a legitimate entry:

function draw(b )
  b.draw()
end

function bounds(b, x, y)
  if x >= b.x and x <= b.X and y >= b.y and y <= b.Y then return true end
end

As long as you pass it the correct data, structured the correct way, it meets the criteria. The user is responsible for creating the function to draw the button, so functions that don't center the text are improper usage. All of the data of the button (including the draw function) is created by the user, so they are allowed to change the colors. There is a function for determining if a click is within the bounds of the button. All three conditions are met at 102 characters (without white space).

#31 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 16 August 2013 - 11:41 PM

View Postimmibis, on 16 August 2013 - 11:32 PM, said:

I think the button API needs clearer requirements - e.g. can the buttons be arbitrary width (Bubba's is always 4 + text length)?
Are exported function names allowed to be shortened to save space? (buttons.draw -> buttons.d etc)
Maybe you could specify the public interface of the API (e.g.
function buttons.new(x, y, width, height, text, fgCol, bgCol) -> buttonObject
function buttons.draw(buttonObject)
function buttons.check(buttonObject, x, y)

Well as it is currently, button padding can be arbitrary or constant as long as the text is centered. That being said, I think there should be a good way to change response requirements based on popular vote. If more than three (3) people up-vote a suggestion to change the response, then I'll change add/remove said suggestion. Please don't reply to it, just upvote. That way I can keep the thread relatively clean :)

View PostLyqyd, on 16 August 2013 - 11:41 PM, said:

Well, if we wanted to get ridiculously pedantic about the wording of the button API challenge, this would be a legitimate entry:

function draw(b )
  b.draw()
end

function bounds(b, x, y)
  if x >= b.x and x <= b.X and y >= b.y and y <= b.Y then return true end
end

As long as you pass it the correct data, structured the correct way, it meets the criteria. The user is responsible for creating the function to draw the button, so functions that don't center the text are improper usage. All of the data of the button (including the draw function) is created by the user, so they are allowed to change the colors. There is a function for determining if a click is within the bounds of the button. All three conditions are met at 102 characters (without white space).

I didn't even think about that. Very clever!

#32 immibis

    Lua God

  • Members
  • 1,033 posts
  • LocationWellington, New Zealand

Posted 16 August 2013 - 11:51 PM

View PostLyqyd, on 16 August 2013 - 11:41 PM, said:

Well, if we wanted to get ridiculously pedantic about the wording of the button API challenge, this would be a legitimate entry:

function draw(b )
  b.draw()
end

function bounds(b, x, y)
  if x >= b.x and x <= b.X and y >= b.y and y <= b.Y then return true end
end

As long as you pass it the correct data, structured the correct way, it meets the criteria. The user is responsible for creating the function to draw the button, so functions that don't center the text are improper usage. All of the data of the button (including the draw function) is created by the user, so they are allowed to change the colors. There is a function for determining if a click is within the bounds of the button. All three conditions are met at 102 characters (without white space).
You forgot to shrink function names.
function d(b)b.d()end
function b(b,x,y)return b.b(x,y)end
is 53 non-whitespace chars.

Edit: or even (27)
function d(b,...)b.d(...)end
where d( B) draws a button and d(b,x,y) checks coordinates and saves the result in b.ok

Edit 2: or even 0 chars

is enough for users to do that (but falls down on "provides a function")



#33 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 16 August 2013 - 11:55 PM

So anyway, that silliness goes to show that the conditions for the button API are a little ambiguous.

I'd say that the button API must not rely on the user to provide any functions, only numbers or strings, and must include a function to check bounds named "check", a function to draw the buttons named "draw", and a function to create a new button named "add" or "new", and allow buttons to be created with custom color backgrounds.

#34 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 16 August 2013 - 11:57 PM

@Lyqyd, I thought about that too, but I didn't want to be a smartass in making my entry. :lol:

#35 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 16 August 2013 - 11:59 PM

Heh okay guys. I'll add a few more rules to the button API requirements.
Edit: Done. With the current requirements, my program is still the #1 I believe. However, I don't think it would take much for Immibis to shave a few characters off of his program and beat mine.

#36 immibis

    Lua God

  • Members
  • 1,033 posts
  • LocationWellington, New Zealand

Posted 17 August 2013 - 12:17 AM

Buttons, 239:
local s,T=" ",term
function new(B,x,y,w,t,f,B)/>_=(w-t:len())/2 t=s:rep(math.floor(_))..t..s:rep(math.ceil(_))
function B.drw()T.setCursorPos(x,y)T.setTextColor(f)T.setBackgroundColor(b)T.write(t)end
function B.chk(X,Y)return y==Y and X&gt;=x and X<x+w end end

Ignore the /> caused by crappy forum software.

Test code:
Spoiler
Still supports arbitrary amounts of padding.</x+w>

#37 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 17 August 2013 - 12:22 AM

For the record, whitespace isn't counted, so you don't really have to condense your code into 3-4 lines :P

#38 immibis

    Lua God

  • Members
  • 1,033 posts
  • LocationWellington, New Zealand

Posted 17 August 2013 - 12:25 AM

View PostKingdaro, on 17 August 2013 - 12:22 AM, said:

For the record, whitespace isn't counted, so you don't really have to condense your code into 3-4 lines :P/>
But I'm already used to doing that!

#39 immibis

    Lua God

  • Members
  • 1,033 posts
  • LocationWellington, New Zealand

Posted 17 August 2013 - 12:36 AM

74 chars packager (or any other program):
http://pastebin.com/BuGmiRrY

Creates a global function package(dirPath) -> packagedString.
Doesn't write it to a file, the caller is responsible for that.

#40 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 17 August 2013 - 12:57 AM

Heh, whitespace abuse is a good way to cheat anything down to a certain point. It's meaningful data in this case, so you'd have to count the whitespace in the block quote to get a more accurate picture. :P





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users