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 »

NickRock wrote:Ah damn I'm sorry, I just wanted to make sure that I didn't make any mistakes explaining the puzzle :(
No worries, that was a nice puzzle though.
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
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Code Puzzles

Post by zorg »

zorg wrote:Edit2: After the answer being posted, i realized that defining the function in that chunk means that the function can access the two vars as upvalues, if one doesn't pass them in as arguments. (The answer though is wrong, since they are being passed in, and the printing happens -inside- the function; it would fail (as in, give the wrong answer) if it was called outside, after calling swap.

Code: Select all

local a = 10
local b = 15
function swap(a, b)
   a = a + b
   b = a - b
   a = a - b
   print("Current value of a is " .. a .. "\nCurrent value of b is " .. b)
end
swap(a,b)
print("Current value of a is " .. a .. "\nCurrent value of b is " .. b)
Put that into http://www.lua.org/cgi-bin/demo
Then try with this:

Code: Select all

local a = 10
local b = 15
function swap()
   a = a + b
   b = a - b
   a = a - b
   print("Current value of a is " .. a .. "\nCurrent value of b is " .. b)
end
swap(a,b)
print("Current value of a is " .. a .. "\nCurrent value of b is " .. b)
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
NickRock
Citizen
Posts: 76
Joined: Thu Dec 25, 2014 9:33 pm
Location: Earth
Contact:

Re: Code Puzzles

Post by NickRock »

Oh that explains everything, well looks like I did something wrong sorry :P
Weeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeooow!!
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Code Puzzles

Post by davisdude »

Ranguna259 wrote:I think you need to give use more information about the problem, I bet this was not presented like this in the euler project.
I pretty much explained it in the code's comments, but here's the actual post.
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
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 »

davisdude wrote:
Ranguna259 wrote:I think you need to give use more information about the problem, I bet this was not presented like this in the euler project.
I pretty much explained it in the code's comments, but here's the actual post.
Ahh, I thought that the number of consecutive digits were the number of digits of the product, that as kinda dumb because the second product has 11 digits and not 13 :rofl:

Code: Select all

function getLargestProduct(data,n)
	local digits = {}
	local product = 1
	local largestPossible = 9^n --largest possible product 
	local largestPreviouse = 0
	for number in data:gmatch('%d') do --iterates the data string number by number
		if #digits < n then
			table.insert(digits,number)
		else
			table.remove(digits,1)
			table.insert(digits,number)
		end
		if #digits == n then
			for i,v in ipairs(digits) do
				product = product*v
			end
			if product == largestPossible then
				return product
			else
				largestPreviouse = product > largestPreviouse and product or largestPreviouse
			end
			product = 1
		end
	end
	return largestPreviouse
end
zorg wrote: ...

Code: Select all

local a = 10
local b = 15
function swap()
   a = a + b
   b = a - b
   a = a - b
   print("Current value of a is " .. a .. "\nCurrent value of b is " .. b)
end
swap(a,b)
print("Current value of a is " .. a .. "\nCurrent value of b is " .. b)
You don't really need to pass a and b to the swap function since it has access to the scope where both variables were created.
This would work too:

Code: Select all

local a = 10
local b = 15
function swap()
   a = a + b
   b = a - b
   a = a - b
   print("Current value of a is " .. a .. "\nCurrent value of b is " .. b)
end
swap()
print("Current value of a is " .. a .. "\nCurrent value of b is " .. b)
But what if the variables had arbitrary names like abel and bill ?

Code: Select all

local abel = 15
local bill = 10
swap(abel,bill)
print(abel) --10
print(bill) --15
We'd probably have to use the debug library.
Last edited by Ranguna259 on Tue Dec 08, 2015 3:59 pm, edited 1 time in total.
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
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Code Puzzles

Post by zorg »

Ranguna: True, but realize, i only showed that the original puzzle's solution was flawed; the swap function being able to work with arbitrary variables was not a stated goal, nor that it needed to work outside of the chunk where a and b were defined. :3

But yes, to make the swap function work, you either need the debug library, or you could use the FFI to cast the two parameters to int pointers, then swap those inside the function... may or may not work, i haven't tested it, and technically, it may break the "no temporary variable" rule as well... and so would the debug lib usage.

p.s., your quote's off a bit :3
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
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 »

zorg wrote:Ranguna: True, but realize, i only showed that the original puzzle's solution was flawed; the swap function being able to work with arbitrary variables was not a stated goal, nor that it needed to work outside of the chunk where a and b were defined. :3
...
Gotcha :D
zorg wrote:...
But yes, to make the swap function work, you either need the debug library, or you could use the FFI to cast the two parameters to int pointers, then swap those inside the function... may or may not work, i haven't tested it, and technically, it may break the "no temporary variable" rule as well... and so would the debug lib usage.
...
That rule possibly renders the puzzle impossible to solve, at least in Lua.
zorg wrote:...
p.s., your quote's off a bit :3
It is ? Damn..
I was going crazy with the editor, it was deleting my post and I ended up pasting everything into a notepad and pasting it back into the editor again, so it's bound to be messed up, sorry about that :P

Finder Puzzle:

Code: Select all

data = "124621257235067"
for number,index1,index2 in data:finder('1241') do
  print(number,index1,index2)
end
--[[Output:
1	1	1
1	6	6
2	2	2
2	5	5
2	7	7
2	10	10
12	1	2
12	6	7
4	3	3
24	2	3
124	1	3
1	1	1
1	6	6
EDIT: I did this one without testing it, so it might prove itself to be difficult..
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.
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 strmt = getmetatable( '' )
strmt.__index['finder'] = function( self, search )
    local searchIndex = 1
    local searchLength = 1
    local searchLast = 0

    return function()
            while searchIndex <= #search do
                local current = search:sub( searchIndex - searchLength + 1, searchIndex )
                local start, stop = self:find( current, searchLast )
                if start then
                    searchLast = stop + 1
                    return current, start, stop
                else
                    if searchLength < searchIndex then
                        searchLength = searchLength + 1
                    else
                        searchIndex = searchIndex + 1
                        searchLength = 1
                    end
                    searchLast = 1
                end
            end
        end
end

data = "124621257235067"
for number, index1, index2 in data:finder('1241') do
    print( number, index1, index2 )
end
There's probably a better way to do this, but oh well.
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
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Code Puzzles

Post by airstruck »

Code: Select all

getmetatable('').__index.finder = function (self, value)
    return coroutine.wrap(function ()
        for i = 1, #value do
            for j = i, 1, -1 do
                for a, b, c in self:gmatch('()(' .. value:sub(j, i) .. ')()') do
                    coroutine.yield(b, a, c - 1)
                end
            end
        end
    end)
end
Still feels like there's an easier way.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Code Puzzles

Post by davisdude »

airstruck wrote:-insert amazing code here-
Still feels like there's an easier way.
I have been defeated...
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
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests