do not reconstruct tree

This commit is contained in:
Felix Van der Jeugt 2021-12-19 00:32:54 +01:00
parent 9efab871b1
commit 1660ee2536
No known key found for this signature in database
GPG Key ID: 58B209295023754D
1 changed files with 3 additions and 30 deletions

View File

@ -76,13 +76,13 @@ function add(a, b)
return reduce(sum)
end
function reconstruct(number)
function magnitude(number)
local function go(d, c)
local left, right
if number[c].d > d then
local left, right
left, c = go(d + 1, c)
right, c = go(d + 1, c)
return { left, right }, c
return 3 * left + 2 * right, c
else
return number[c].n, c + 1
end
@ -90,33 +90,6 @@ function reconstruct(number)
return go(0, 1)
end
function printnumber(a)
local function go(a)
if type(a) == "number" then
io.write(tostring(a))
else
io.write("[")
go(a[1])
io.write(",")
go(a[2])
io.write("]")
end
end
go(reconstruct(a))
io.write("\n")
end
function magnitude(a)
local function go(a)
if type(a) == "number" then
return a
else
return 3 * go(a[1]) + 2 * go(a[2])
end
end
return go(reconstruct(a))
end
local numbers = {}
for line in io.lines(arg[3]) do
table.insert(numbers, parse(line))