Code Puzzles

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Code Puzzles

Post by Ranguna259 »

Both are correct. airstruck that code is increadible, I never thought of doing it that way, great work :D
We are running out of puzzles..
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
kbmonkey
Party member
Posts: 138
Joined: Tue Sep 01, 2015 12:19 pm
Location: Sydney
Contact:

Re: Code Puzzles

Post by kbmonkey »

What's going on here? These puzzles look fun, can we have another, please? :awesome:
User avatar
zorg
Party member
Posts: 3449
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Code Puzzles

Post by zorg »

here's one, good luck :3

Code: Select all


local z,o,r,g = require(("%s.%s"):format(select(2,...)))() -- edit2: added that last set of parens
print(loadstring((table.concat{z(g[1]) .. o(r[-1]) .. r(o[0]) .. g(z[ [[]] ])}):reverse())())

-- should return these five strings:
-- "Just", "another", "lua", "hackonaut", "."

-- Hints:
-- - Feel free to name the required file anything, it doesn't matter... much.
-- - Don't let the output misdirect your efforts.

Edit: I realize the above one might be a tad hard and or arbitrary, so here's a much easier one:

Code: Select all


require "sm"

local a,b = sm(2)
local c = sm() -- same as sm(1)
local d,e = sm(2)

a("Four")
b(4)
c(a+1)
d(-0.0), e(1.0)

print(a + b, a - b, a * b, b / a, a % b, a ^ b)
-- "Eight" "Zero" "Sixteen" "One", "Null", "Two-hundred fifty-six"

print(a .. c, a*c+c*c)
-- "Forty-five" "Forty-five"

print(b .. c == b*c+c*c)
-- true

print(a/(e .. d)/c) -- edit3: fixed
-- "Zero dot zero eight"

print(a/c .. c/b) --edit3: added
-- 81.25 
Edit2: As pointed out by airstruck, require returns only one result, so i edited the first puzzle a tiny bit; now it should be possible without redefining require... sorry about that :3 btw, your answer is an awesome abuse of (nonexistent) rules example, congrats!
Edit3: Fixed a minor error in the definition, and added the last one.
Last edited by zorg on Thu Dec 10, 2015 10:07 am, edited 5 times in total.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Code Puzzles

Post by airstruck »

Well, require only returns a single value, so I guess you'll have to overwrite it?

Code: Select all

function require ()
    print("Just", "another", "lua", "hackonaut", ".")
    os.exit()
end
:p
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Code Puzzles

Post by airstruck »

Here's one...

Code: Select all

T(function (T) print 'fuzzy wuzzy'
    T(function (T) print 'was a bear' end)
    T(function (T) print 'had no hair' end)
end)
T(function (T) print 'was'
    T(function (T) print 'not fuzzy' end)
end)
T(function (T) print 'he' end)
Output:
fuzzy wuzzy
was a bear
fuzzy wuzzy
had no hair
fuzzy wuzzy
was
not fuzzy
was
he
Define T.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Code Puzzles

Post by davisdude »

Code: Select all

local oldprint = print

T = setmetatable( {}, { 
    __call = function( _, func )
        func( _ )
        for i = 1, #T do
            oldprint( T[i] )
        end
        table.remove( T, #T )
        for i = #T, 2, -1 do
            oldprint( T[i] )
        end
    end
} )

function print( str )
    table.insert( T, str )
end
This re-writes print, not sure if that's okay, but I couldn't figure it out any other way... :P
It also doesn't work if T is nested any deeper.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
kbmonkey
Party member
Posts: 138
Joined: Tue Sep 01, 2015 12:19 pm
Location: Sydney
Contact:

Re: Code Puzzles

Post by kbmonkey »

zorg wrote:here's one, good luck :3
Now I am sorry I asked! He he. First time I encountered the "select" keyword, and "loadstring" too. Will stew in it a while :crazy:
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Code Puzzles

Post by vrld »

Another solution:

Code: Select all

function T(f)
  local msg
  setfenv(f, setmetatable({print = function(s) msg=s print(s) end}, {__index = _G}))
  f(function(f) T(f) print(msg) end)
end
New puzzle:

Code: Select all

function double(x) return 2*x end
function rep(s) return s..s end
print((10 * double)(2))
print((3 * rep)"na", "BATMAN")
Output:
2048
nananananananana BATMAN
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Code Puzzles

Post by bartbes »

Easy!

Code: Select all

debug.setmetatable(function() end,
{
	__mul = function(amount, f)
		return function(input)
			for i = 1, amount do
				input = f(input)
			end
			return input
		end
	end,
})
Or if you're opposed to debug.setmetatable, set a metatable on _G that wraps functions when they are set, left as an exercise for the reader.
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Code Puzzles

Post by airstruck »

Interesting solutions to the fuzzy wuzzy puzzle. Using setfenv is pretty clever. It can be done without messing with print, but the solutions won't be so minimal. There are some practical (if somewhat obscure) applications for the non-tricky solution.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 1 guest