Code Puzzles

General discussion about LÖVE, Lua, game development, puns, and unicorns.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Code Puzzles

Post by davisdude »

airstruck wrote:It can be done without messing with print, but the solutions won't be so minimal.
I'm interested in seeing how it's done without redefining print.
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
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Code Puzzles

Post by vrld »

bartbes wrote: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.
:joker:
davisdude wrote:I'm interested in seeing how it's done without redefining print.
Here is one:

Code: Select all

function T(f)
    f(function(g) g(T) f(function()end) end)
end
Edit: A puzzle!

Code: Select all

s{
function(w)
	w(6)
	print("not")
end,
function(w)
	for i = 1,4 do
		print("fuzzy")
		w(2)
	end
end,
function(w)
	for i = 1,3 do
		print("wuzzy")
		w(2)
	end
end,
function(w)
	w(1)
	print("was")
	w(2)
	for i = 1,2 do
		w(2)
		print("was")
	end
end,
function(w)
	w(1)
	print("a bear")
	w(2)
	print("had no hair")
	w(4)
	print("he")
end
}
Output:
fuzzy
wuzzy
was
a bear
fuzzy
wuzzy
had no hair
fuzzy
wuzzy
was
not
fuzzy
was
he
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Code Puzzles

Post by davisdude »

vrld wrote:
davisdude wrote:I'm interested in seeing how it's done without redefining print.
Here is one:

Code: Select all

function T(f)
    f(function(g) g(T) f(function()end) end)
end
Although this doesn't work if it's nested any more than two levels.
(When I say "work," I mean that it doesn't repeat the printed statement above then below, which is how I expect it to work, at least).
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
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Code Puzzles

Post by vrld »

davisdude wrote:Although this doesn't work if it's nested any more than two levels.
(When I say "work," I mean that it doesn't repeat the printed statement above then below, which is how I expect it to work, at least).
It solves the puzzle ... :P

Here is a solution that allows nesting:

Code: Select all

function T(f)
	local function t(g)
		T(g)
		f(function() end)
	end
	f(t)
end
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
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 »

Now that looks like a really nice solution.

Might be time to rewrite knife.test, thanks ;)
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests