Jump to content




Lua Code Golf


114 replies to this topic

#81 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 07 November 2013 - 08:26 AM

View PostAnavrins, on 31 October 2013 - 02:48 AM, said:

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.
actually you can:
(x < 0) and (function() for i = 1, 2 do print( "looping"  ) end end)() or (function() return "return" end )()

Edited by Engineer, 07 November 2013 - 08:26 AM.


#82 Anavrins

  • Members
  • 775 posts

Posted 07 November 2013 - 10:49 AM

View PostEngineer, on 07 November 2013 - 08:26 AM, said:

actually you can:
(x < 0) and (function() for i = 1, 2 do print( "looping"  ) end end)() or (function() return "return" end )()

Of course you can do this, but that's not what he did and his code just throw an error, thus his submission is invalid.

Edited by Anavrins, 07 November 2013 - 10:51 AM.


#83 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 07 November 2013 - 11:00 AM

Okay guys, I finished updating the challenge list/winning scores. Let me know if I missed anything.

#84 AgentE382

  • Members
  • 119 posts

Posted 07 November 2013 - 03:29 PM

View PostBubba, on 07 November 2013 - 11:00 AM, said:

Okay guys, I finished updating the challenge list/winning scores. Let me know if I missed anything.

My username is mistakenly "AgentA382" in the main post. Other than that, thanks for updating! :D

#85 Symmetryc

  • Members
  • 434 posts

Posted 07 November 2013 - 03:53 PM

View PostAgentE382, on 04 November 2013 - 06:42 AM, said:

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
Char Count: 78 :P
function f(x)
for i = x, 1, -1 do
b = 1
for j = 2, i - 1 do
b = i % j ~= 0 and b
end
if b then
return i
end
end
end
Edit: Not that it matters, but your code is also about 16.5x slower than mine :P.

Edited by Symmetryc, 07 November 2013 - 04:01 PM.


#86 AgentE382

  • Members
  • 119 posts

Posted 07 November 2013 - 06:31 PM

View PostSymmetryc, on 07 November 2013 - 03:53 PM, said:

View PostAgentE382, on 04 November 2013 - 06:42 AM, said:

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
Char Count: 78 :P
function f(x)
for i = x, 1, -1 do
b = 1
for j = 2, i - 1 do
b = i % j ~= 0 and b
end
if b then
return i
end
end
end
Edit: Not that it matters, but your code is also about 16.5x slower than mine :P.
Remember, the goal is short, not fast. :D
function f(x)
   for i = 2, x do
      b = 1
      for j = 2, i - 1 do
         b = i % j ~= 0 and b
      end
      p = b and i or p
   end
   return p
end
75 chars.

Edited by AgentE382, 07 November 2013 - 06:49 PM.


#87 bentallea

  • Members
  • 19 posts

Posted 07 November 2013 - 07:47 PM

Turtle goto function (335 chars and works)
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
if x < 0 then
  for a = -1, x, -1 do t.back() end
else
  for a = 1, x do f() end
end
if y < 0 then
  for a = -1,y,-1 do t.down() end
else
  for a = 1,y do t.up() end
end
z < 0 and t.turnLeft() or t.turnRight()
if z < 0 then
  for a = -1, z -1 do f() end
else
  for a = 1, z do f() end
end
end


#88 Symmetryc

  • Members
  • 434 posts

Posted 07 November 2013 - 09:31 PM

View PostAgentE382, on 07 November 2013 - 06:31 PM, said:

View PostSymmetryc, on 07 November 2013 - 03:53 PM, said:

View PostAgentE382, on 04 November 2013 - 06:42 AM, said:

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
Char Count: 78 :P
function f(x)
for i = x, 1, -1 do
b = 1
for j = 2, i - 1 do
b = i % j ~= 0 and b
end
if b then
return i
end
end
end
Edit: Not that it matters, but your code is also about 16.5x slower than mine :P.
Remember, the goal is short, not fast. :D
function f(x)
   for i = 2, x do
	  b = 1
	  for j = 2, i - 1 do
		 b = i % j ~= 0 and b
	  end
	  p = b and i or p
   end
   return p
end
75 chars.
>.> You're a very tricky.

71 Chars
function f(x)
for i = 2, x do
b = i
for j = 2, i - 1 do
b = i % j ~= 0 and b
end
p = b or p
end
return p
end

Beat that :P

#89 AgentE382

  • Members
  • 119 posts

Posted 08 November 2013 - 05:33 PM

Done :D
function f(x)
   for i = 2, x do
      b = i
      for j = 2, i / 2 do
         b = i % j > 0 and b
      end
      p = b or p
   end
   return p
end
70 chars.
BTW, I made it faster, too! :D

Edited by AgentE382, 08 November 2013 - 05:34 PM.


#90 Symmetryc

  • Members
  • 434 posts

Posted 09 November 2013 - 11:20 AM

View PostAgentE382, on 08 November 2013 - 05:33 PM, said:

Done :D
function f(x)
   for i = 2, x do
	  b = i
	  for j = 2, i / 2 do
		 b = i % j > 0 and b
	  end
	  p = b or p
   end
   return p
end
70 chars.
BTW, I made it faster, too! :D
I have to admit that was pretty clever lol.
function g(x)
b = x
for i = 2, x / 2 do
b = x % i > 0 and b
end
return b or g(x - 1)
end
59 Chars
(88x faster!)

#91 jay5476

  • Members
  • 289 posts

Posted 14 November 2013 - 01:49 AM

A menu function that must
- Put Options in center of screen (x and y)
- Return a value to help know what was selected
- Use up/down to move and enter to select ( I could easily shorten by using 1,2,3 and to control )

MY Code: 252 no whitespace
T = term
w,h = T.getSize()
function m(t)
  l = h/2-#t/2
  s = 1
  repeat
	T.clear()
	for i = 1, #t do
	  p = (s == i and "> "..t[i].." <" or "  "..t[i].."  ")
	  T.setCursorPos((w+1)/2-#p/2,l+i)
	  write(p)
	end
	_,k = os.pullEvent()
	s = k == 200 and s-1 or s
	s = k == 208 and s+1 or s
	s = s < 1 and #t or s
	s = s > #t and 1 or s
  until k == 28
  return s
end
Usage
result = menu(table) --# returns index of option selected in table
Good Luck People

Edited by jay5476, 14 November 2013 - 01:50 AM.


#92 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 14 November 2013 - 02:21 AM

My submission for jay5476's menu challenge.

Char count: 229
t = term
function m(a)
  w,h = t.getSize()
  q = #a
  s = 1
  repeat
	t.clear()
	for i = 1, q do
	  p = s == i and "["..a[i].."]" or a[i]
	  t.setCursorPos(w/2-#p/2,h/2-q/2+i)
	  write(p)
	end
	_,k = os.pullEvent()
	s = k == 200 and s-1 or k == 208 and s+1 or s
	s = s < 1 and q or s > q and 1 or s
  until k == 28
  return s
end

My test code
print(m({"one", "two", "three", "truck"}))

My submission for bentallea's AES challenge

bentallea's submission was 13540 chars.

My submission: 12049

Edited by theoriginalbit, 14 November 2013 - 02:55 AM.


#93 jay5476

  • Members
  • 289 posts

Posted 14 November 2013 - 03:53 PM

Damn BIT, I might just have to try harder... but it already seems challenging

Super EDIT:
t = term
function m(a)
  w,h = t.getSize()
  q = #a
  s = 1
  while k ~= 28 do
	    t.clear()
	    for i = 1, q do
		  p = s == i and "["..a[i].."]" or a[i]
		  t.setCursorPos(w/2-#p/2,h/2-q/2+i)
		  write(p)
	    end
	    _,k = os.pullEvent()
	    s = k == 200 and s-1 or k == 208 and s+1 or s
	    s = s < 1 and q or s > q and 1 or s
  end
  return s
end
228 Characters

Edited by jay5476, 14 November 2013 - 03:56 PM.


#94 AgentE382

  • Members
  • 119 posts

Posted 14 November 2013 - 05:04 PM

Preliminary shortening:
t = term
function m(a)
  w,h = t.getSize()
  q = #a
  s = 0
  while k ~= 28 do
    t.clear()
    for i = 1, q do
      b = a[i]
      p = s + 1 == i and "["..b.."]" or b
      t.setCursorPos(w/2-#p/2,h/2-q/2+i)
      write(p)
    end
    _,k = os.pullEvent()
    s = (k == 200 and s-1 or k == 208 and s+1 or s) % q
  end
  return s + 1
end
215 chars

EDIT: First version didn't work. Fixed. Sorry if you saw that.

Edited by AgentE382, 14 November 2013 - 05:33 PM.


#95 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 14 November 2013 - 08:17 PM

View Postjay5476, on 14 November 2013 - 03:53 PM, said:

Super EDIT:
t = term
function m(a)
  w,h = t.getSize()
  q = #a
  s = 1
  while k ~= 28 do
		t.clear()
		for i = 1, q do
		  p = s == i and "["..a[i].."]" or a[i]
		  t.setCursorPos(w/2-#p/2,h/2-q/2+i)
		  write(p)
		end
		_,k = os.pullEvent()
		s = k == 200 and s-1 or k == 208 and s+1 or s
		s = s < 1 and q or s > q and 1 or s
  end
  return s
end
228 Characters
You need to test your code. That will only work once, attempting to use it a second time it will not run, it will just return the previously selected option

New submission for jay5476's challenge

207 Chars
t = term
function m(a)
  w,h = t.getSize()
  q = #a
  s = 1
  repeat
    t.clear()
    for i = 1, q do
      p = s == i and ">"..a[i] or a[i]
      t.setCursorPos(w/2-#p/2,h/2-q/2+i)
      write(p)
    end
    _,k = os.pullEvent()
    s = (k == 200 and s-1 or k == 208 and s+1 or s) % q
  until k == 28
  return s
end

If we don't care about it not running a second time then 206 Chars.
t = term
function m(a)
  w,h = t.getSize()
  q = #a
  s = 1
  while k ~= 28 do
    t.clear()
    for i = 1, q do
      p = s == i and ">"..a[i] or a[i]
      t.setCursorPos(w/2-#p/2,h/2-q/2+i)
      write(p)
    end
    _,k = os.pullEvent()
    s = (k == 200 and s-1 or k == 208 and s+1 or s) % q
  end
  return s
end

Jay I would like your input on whether you don't care if it runs a second time so that Bubba can put the correct count in :)

Edited by theoriginalbit, 14 November 2013 - 08:23 PM.


#96 AgentE382

  • Members
  • 119 posts

Posted 14 November 2013 - 09:34 PM

Bit, you need to test your code, too. Due to the modulus, it will never display the cursor on the last line.

t = term
function m(a)
  w,h = t.getSize()
  q = #a
  s = 0
  while k ~= 28 do
    t.clear()
    for i = 1, q do
      p = s + 1 == i and ">"..a[i] or a[i]
      t.setCursorPos(w/2-#p/2,h/2-q/2+i)
      write(p)
    end
    _,k = os.pullEvent()
    s = (k == 200 and s-1 or k == 208 and s+1 or s) % q
  end
  return s + 1
end
210 char version of Bit's 206 char submission.

#97 Symmetryc

  • Members
  • 434 posts

Posted 14 November 2013 - 10:18 PM

t = term
function m(a)
  w,h = t.getSize()
  s = 1
  while k ~= 28 do
	t.clear()
	for i = 1, #a do
	  p = s == i - 1 and ">"..a[i] or a[i]
	  t.setCursorPos(w/2-#p/2,h/2-#a/2+i)
	  write(p)
	end
	_,k = os.pullEvent()
	s = (k == 200 and s-1 or k == 208 and s+1 or s) % #a
  end
  return s
end
209 characters :D
Edit: 206
Edit 2: Just a note, its return starts from 0 and goes to #a - 1 rather than from 1 to #a

Edited by Symmetryc, 14 November 2013 - 10:30 PM.


#98 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 14 November 2013 - 10:23 PM

Ok so since we're all doing the while loop, I'm just going to assume that we don't care that it can only run once... However I'll still provide both :P

Can run only once (which is a bug present in everyones solutions that use a while loop)... 208 Chars
t = term
function m(a)
  q = #a
  w,h = t.getSize()
  s = 1
  while k ~= 28 do
	t.clear()
	for i = 1, q do
	  p = s == i-1 and ">"..a[i] or a[i]
	  t.setCursorPos(w/2-#p/2,h/2-q/2+i)
	  write(p)
	end
	_,k = os.pullEvent()
	s = (k == 200 and s-1 or k == 208 and s+1 or s) % q
  end
  return s
end

Can run many times... 209 Chars
t = term
function m(a)
  q = #a
  w,h = t.getSize()
  s = 1
  repeat
	t.clear()
	for i = 1, q do
	  p = s == i-1 and ">"..a[i] or a[i]
	  t.setCursorPos(w/2-#p/2,h/2-q/2+i)
	  write(p)
	end
	_,k = os.pullEvent()
	s = (k == 200 and s-1 or k == 208 and s+1 or s) % q
  until k == 28
  return s
end

Edited by theoriginalbit, 14 November 2013 - 10:24 PM.


#99 jay5476

  • Members
  • 289 posts

Posted 15 November 2013 - 01:38 AM

For my challenge it needs to run more then once. I made the mistake and didnt see it would only run once

#100 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 15 November 2013 - 08:02 AM

View Postjay5476, on 15 November 2013 - 01:38 AM, said:

For my challenge it needs to run more then once. I made the mistake and didnt see it would only run once
In that case my new submission of 208 Chars is in the lead
t = term
function m(a)
  w,h = t.getSize()
  s = 1
  repeat
    t.clear()
    for i = 1, #a do
      p = s == i-1 and ">"..a[i] or a[i]
      t.setCursorPos((w-#p)/2,(h-#a)/2+i)
      write(p)
    end
    _,k = os.pullEvent()
    s = (k == 200 and s-1 or k == 208 and s+1 or s) % #a
  until k == 28
  return s
end

View PostSymmetryc, on 14 November 2013 - 10:18 PM, said:

Edit: 206
That code is 207, not 206.

Edited by theoriginalbit, 15 November 2013 - 08:05 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users