Jump to content




Lua Code Golf


114 replies to this topic

#61 ElvishJerricco

  • Members
  • 803 posts

Posted 06 October 2013 - 05:03 PM

View Postbentallea, on 06 October 2013 - 04:53 PM, said:

for a quine i have 2 possible ideas:
  •  return "return" 
    = 14 chars
  •  print("print(\"\")") 
    = 20 chars
run either one as a program should yield the source code

Neither of those works. Even if the return value were printed (which it's not unless you're in the Lua shell), the first would print
return
and the second would print
print("")

Neither is equivalent to their source codes.

#62 Symmetryc

  • Members
  • 434 posts

Posted 06 October 2013 - 05:33 PM

View PostElvishJerricco, on 17 August 2013 - 11:28 AM, said:

View Posttheoriginalbit, on 17 August 2013 - 06:16 AM, said:

My top level coroutine override.

Characters
82

Note
I even cleanup my temp variable (75 characters if I don't cleanup)

Code
os.queueEvent("modem_message")
p=pcall
pcall=function()
  pcall=p
  p=nil
  os.run({},"a")
end

Note 2
pcall is called after printError is...

Won't work as written. You're just setting the pcall in the shell environment, not the _G environment. Also, it takes one fewer character to do function a() than a = function().

Written so it works,

Characters
82

Code
os.queueEvent("modem_message")
p=pcall
function _G.pcall()
  _G.pcall=p
  os.run({},"n")
end

EDIT: actually, not sure why you did p = nil. It's no big deal if you leave a random p=pcall in the shell environment that's never going to be used again. Especially since this competition is about making code as short as possible =P Edited the code and now its 82 again instead of 87.
You can shorten it even more by removing parenthesis.
Char Count: 80
os.queueEvent"modem_message"
p=pcall
function _G.pcall()
  _G.pcall=p
  os.run({},"n")
end

Edit: I think this might work actually work too...? (If it does consider this my submission rather than the former :P)
Char Count: 78
os.queueEvent"modem_message"
p=pcall
function _G.pcall()
  _G.pcall=p
  shell.run"n"
end


#63 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 06 October 2013 - 05:39 PM

View Postkreezxil, on 06 October 2013 - 11:43 AM, said:

View Postbentallea, on 06 October 2013 - 10:09 AM, said:

this is for the turtle goto challenge
instructions:
  • place turtle facing west
  • insert fuel into any slot
  • run goto(x,y,z,)
NOTE: if there is any data in b then the coordinates are assumed global. leave b out of the function if the coordinates are relative to the turtle.
Spoiler
total length: 447 characters without whitespace
this program won't run it's only a function, You need the interface code for parsing the command line. it's character count will be more than 447. At minimum you will need this at the end of the program to make it functional:
a={...}
goto(a[1],a[2],a[3],a[4] or nil)
Also there is an error on line 1 of your function() you have 'B' and not 'b'. Remember variables are case sensitive. With my addition to make your goto function an actual program for the competition valid your total character count comes to 476!
I do agree with theoriginalbit on this matter. But besides that, you only need to add 9 characters if you insist on it being a program:
goto(...)
This keeps the count at 456 which is a lot less than your addition.

And why would you ever do this?
a[4] or nil
It's like doing '1+0'. :P

#64 Symmetryc

  • Members
  • 434 posts

Posted 06 October 2013 - 05:49 PM

View PostOrwell, on 06 October 2013 - 05:39 PM, said:

And why would you ever do this?
a[4] or nil
It's like '1+0'. :P
In this instance it's not needed, but it does have a purpose.

Let's say we want to make something like this:
local textTable = <stuff>
local setText( a, b, t )
  <stuff>
end
local f = function( t )
  local text = t or textTable[1][2] --#or nil
  textTable[3][4] = text
end
If textTable[1][2] is nil and the optional param t isn't given (so it is nil), text will result in false rather than nil, which can cause textTable[3][4] to become false rather than nil, so when you do pairs(textTable[3]), you will get a key value pair that is 4 and false, where as if you were to put "or nil", textTable[3][4] would become nil and it wouldn't show up in the pairs(textTable[3]) loop. Hopefully that made sense lol.

#65 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 06 October 2013 - 06:09 PM

View PostSymmetryc, on 06 October 2013 - 05:49 PM, said:

* snip *
That doesn't seem to be entirely true, 'nil or nil' yields nil. Where nil does make a difference is in the case of 'false or nil':
x = 1==2
y = x --# or nil
Of course, in the case of the golf submission, b is only used in the conditional statement 'if b then' so it wouldn't matter in this particular example.
But you are right about it having a use. I didn't realize that. :)

#66 Symmetryc

  • Members
  • 434 posts

Posted 06 October 2013 - 06:25 PM

View PostOrwell, on 06 October 2013 - 06:09 PM, said:

View PostSymmetryc, on 06 October 2013 - 05:49 PM, said:

* snip *
That doesn't seem to be entirely true, 'nil or nil' yields nil. Where nil does make a difference is in the case of 'false or nil':
x = 1==2
y = x --# or nil
Of course, in the case of the golf submission, b is only used in the conditional statement 'if b then' so it wouldn't matter in this particular example.
But you are right about it having a use. I didn't realize that. :)
Yeah that's what I meant I guess lol, I was just telling BIT the same thing on Skype a week ago, but I guess I didn't remember the whole scenario XD.

#67 bentallea

  • Members
  • 19 posts

Posted 06 October 2013 - 11:23 PM

update to the turtle goto program
steps:
place turtle facing west
put fuel in slot 1
run g(x,y,z, B)

function g(x, y, z, j)
if j then
c,d,e = gps.locate()
x,y,z = x-c,y-d,z-e
end
t=turtle
f=t.forward
t.select(1)
t.refuel()
if x+y+z > t.getFuelLevel() then
(x < 0) and for a = -1, x, -1 do t.back() end or for a = 1, x do f() end
(y < 0) and for a = -1,y,-1 do t.down() end or for a = 1,y do t.up() end
(z < 0) and t.turnLeft() or t.turnRight()
(z < 0) and for a = -1, z -1 do f() end or for a = 1, z do f() end
end
end
this is 321 characters using bubba's recommended counter, compared to 337 of my old submission using the same counter.

EDIT: changed to j from b to avoid b being edited to an emoticon

#68 AgentE382

  • Members
  • 119 posts

Posted 07 October 2013 - 07:02 AM

Button API submission:

T = term
function b(t,x,y,c,g,p)
	return function(X,Y)
		if X then
			return X >= x and Y == y and X <= x + 2 * #p + #t
		else
			T.setCursorPos(x,y)
			T.setTextColor(c)
			T.setBackgroundColor(g)
			T.write(p..t..p)
		end
	end
end

Closures FTW! 174 chars.

EDIT: If I don't allow custom padding, I can shave off 3 chars for 171:
Spoiler

Call the function to create a button object. Call the button object with no parameters to draw it, or call it with click coordinates to check its bounds.

C++ style class definition:
Spoiler

Test code (my one test, plus immibis' and Sorroko's):

Spoiler


EDIT2: To cover all bases, I can rewrite this to return 2 separate functions in a table for 177 chars:
Spoiler

Edited by AgentE382, 07 October 2013 - 04:33 PM.


#69 M4sh3dP0t4t03

  • Members
  • 255 posts
  • LocationGermany

Posted 07 October 2013 - 01:33 PM

View PostFreack100, on 06 October 2013 - 03:22 PM, said:

My attempt at a quine:
 


Run this as a program and it will return it own sourcecode.

Total characters: 0

That is no program. First sentence in the Wikipedia article for "Computer program":

Quote

A computer program, or just a program, is a sequence of instructions, written to perform a specified task with a computer.
And because your "program" doesn't include any instructions to be performed, it isn't a program.

#70 Symmetryc

  • Members
  • 434 posts

Posted 08 October 2013 - 05:40 PM

Button API Submission:
Edit: Snipped, Just noticed Agent's post :/.
Edit2: Alright, I compacted it down a bit
Char Count: 164
T = term
function b(t,x,y,c,g,p)
return function(X,Y)
T.setCursorPos(x,y)
T.setTextColor(c)
T.setBackgroundColor(g)
return X and X >= x and Y == y and X <= x + 2 * #p + #t or write(p..t..p)
end
end

Challenge: Write a function that returns the greatest prime number equal to or below n (must be calculated, can't use a list of pre-made primes)

#71 AgentE382

  • Members
  • 119 posts

Posted 10 October 2013 - 07:43 AM

View PostSymmetryc, on 08 October 2013 - 05:40 PM, said:

Button API Submission:
Edit: Snipped, Just noticed Agent's post :/.
Edit2: Alright, I compacted it down a bit
Char Count: 164
T = term
function b(t,x,y,c,g,p)
return function(X,Y)
T.setCursorPos(x,y)
T.setTextColor(c)
T.setBackgroundColor(g)
return X and X >= x and Y == y and X <= x + 2 * #p + #t or write(p..t..p)
end
end

Challenge: Write a function that returns the greatest prime number equal to or below n (must be calculated, can't use a list of pre-made primes)

First, your button code fails my tests. Also, you made me realize that I can shave off 2 chars for 169 chars:
T = term
function b(t,x,y,c,g)
        return function(X,Y)
                if X then
                        return X >= x and Y == y and X <= x + 2 + #t
                else
                        T.setCursorPos(x,y)
                        T.setTextColor(c)
                        T.setBackgroundColor(g)
                        write(" "..t.." ")
                end
        end
end
I also don't really know which one's more convenient, but this returns 2 functions instead of one:
Spoiler

Second, challenge accepted! Primes submission (trial division):
function l(u)
    for n = u,2,-1 do
        for d = 2,n-1 do
            if n % d == 0 then
                f = 1
            end
        end
        if f then f = nil else return n end
    end
end
90 chars.


Yes, I know there are optimizations I left out, but it's the smallest correct code I could think of. Note: I did implement a short Sieve of Eratosthenes, but it's larger, even with everything unnecessary removed and the whole thing condensed as much as possible.

#72 AgentE382

  • Members
  • 119 posts

Posted 10 October 2013 - 08:23 AM

Forget what I said in my last post. Here's my fixed Sieve of Eratosthenes at 81 chars:
function l( b )
    p = {}
    for i = 2, b do
        if not p[i] then
            r = i
            for j = i ^ 2, b, i do
                p[j] = 1
            end
        end
    end
    return r
end


#73 bentallea

  • Members
  • 19 posts

Posted 10 October 2013 - 12:43 PM

how about this for prime less than or equal to n?
function p(u)
i=2
for a = 3,u,2 do
  j=1
  for b = 3, a,2 do
   j=j and b/a%0
  end
  i=j and a
end
return i
end
= 76 chars using repeated primality testing

#74 Symmetryc

  • Members
  • 434 posts

Posted 10 October 2013 - 02:35 PM

View PostAgentE382, on 10 October 2013 - 07:43 AM, said:

View PostSymmetryc, on 08 October 2013 - 05:40 PM, said:

Button API Submission:
Edit: Snipped, Just noticed Agent's post :/.
Edit2: Alright, I compacted it down a bit
Char Count: 164
T = term
function b(t,x,y,c,g,p)
return function(X,Y)
T.setCursorPos(x,y)
T.setTextColor(c)
T.setBackgroundColor(g)
return X and X >= x and Y == y and X <= x + 2 * #p + #t or write(p..t..p)
end
end

Challenge: Write a function that returns the greatest prime number equal to or below n (must be calculated, can't use a list of pre-made primes)

First, your button code fails my tests
Yeah I realized this because if a boolean were to be false in the first chain, it would default to the second :/. Here's my new one:
Char Count: 166
T = term
function b(t,x,y,c,g,p)
return function(B,X,Y)
T.setCursorPos(x,y)
T.setTextColor(c)
T.setBackgroundColor(g)
return B and write(p..t..p) or X >= x and Y == y and X <= x + 2 * #p + #t
end
end


#75 Anavrins

  • Members
  • 775 posts

Posted 31 October 2013 - 02:48 AM

View Postbentallea, on 06 October 2013 - 11:23 PM, said:

update to the turtle goto program
steps:
place turtle facing west
put fuel in slot 1
run g(x,y,z, B)

function g(x, y, z, j)
if j then
c,d,e = gps.locate()
x,y,z = x-c,y-d,z-e
end
t=turtle
f=t.forward
t.select(1)
t.refuel()
if x+y+z > t.getFuelLevel() then
(x < 0) and for a = -1, x, -1 do t.back() end or for a = 1, x do f() end
(y < 0) and for a = -1,y,-1 do t.down() end or for a = 1,y do t.up() end
(z < 0) and t.turnLeft() or t.turnRight()
(z < 0) and for a = -1, z -1 do f() end or for a = 1, z do f() end
end
end
this is 321 characters using bubba's recommended counter, compared to 337 of my old submission using the same counter.

EDIT: changed to j from b to avoid b being edited to an emoticon

There's a syntax error with your code.
You can't have loops of any kind in a ternary statement.

#76 ElvishJerricco

  • Members
  • 803 posts

Posted 03 November 2013 - 12:20 PM

View PostSymmetryc, on 06 October 2013 - 05:33 PM, said:

You can shorten it even more by removing parenthesis.
Char Count: 80
os.queueEvent"modem_message"
p=pcall
function _G.pcall()
  _G.pcall=p
  os.run({},"n")
end

Edit: I think this might work actually work too...? (If it does consider this my submission rather than the former :P)
Char Count: 78
os.queueEvent"modem_message"
p=pcall
function _G.pcall()
  _G.pcall=p
  shell.run"n"
end

I'm a little late here I know, but the point of the top level overrides is to be outside the shell, so shell.running it shouldn't be considered a top level override.

#77 AgentE382

  • Members
  • 119 posts

Posted 04 November 2013 - 06:42 AM

View Postbentallea, on 10 October 2013 - 12:43 PM, said:

how about this for prime less than or equal to n?
function p(u)
i=2
for a = 3,u,2 do
  j=1
  for b = 3, a,2 do
   j=j and b/a%0
  end
  i=j and a
end
return i
end
= 76 chars using repeated primality testing

Absolutely genius method, but unfortunately doesn't work. Try `print(p(10))` and you'll see. I also just realized that I could shave off 2 chars from my sieve for 79 chars!
function l( b )
    p = {}
    for i = 2, b do
        if not p[i] then
            r = i
            for j = i, b, i do
                p[j] = 1
            end
        end
    end
    return r
end


#78 ElvishJerricco

  • Members
  • 803 posts

Posted 05 November 2013 - 08:17 PM

Did a few more ideas with the coroutine override. Found some interesting ways to do it.

This one errors the shell instead of rednet by setting one of its next function calls to error Also, dofile i think is the shortest way to run a file. This one doesn't reset the old functions but n can do that fairly easily. All we need is the override.
Count: 43
_G.read=error
function _G.pcall()
    dofile"n"
end

Alternatively, we don't even really need to kill the top coroutines. We just need to take control. Again, this doesn't reset the old function, but that's ok.
Count: 29
function _G.read()
    dofile"n"
end

Same things with resets
Spoiler


#79 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,462 posts

Posted 07 November 2013 - 01:29 AM

It's not a TLCO if you don't kill the coroutines, so the 29-char one doesn't count.

I should poke Bubba to update the records listing.

#80 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 07 November 2013 - 07:42 AM

View PostLyqyd, on 07 November 2013 - 01:29 AM, said:

It's not a TLCO if you don't kill the coroutines, so the 29-char one doesn't count.

I should poke Bubba to update the records listing.

No need to poke :) I'll update it some time later today.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users