do not reconstruct tree
This commit is contained in:
parent
9efab871b1
commit
1660ee2536
@ -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))
|
||||
|
Loading…
Reference in New Issue
Block a user