require "ext" local lines = {} for line in io.lines() do table.insert(lines,line) end local sum = 0 local loaded = {{{6}},{{2}}} for i = 1,#lines,3 do local l1,l2 = lines[i], lines[i+1] l1 = l1:gsub("%[","{"):gsub("]","}") l2 = l2:gsub("%[","{"):gsub("]","}") l1 = load("return "..l1)() l2 = load("return "..l2)() table.insert(loaded,l1) table.insert(loaded,l2) end local function compare(a,b) -- return true if a comes before b assert(a ~= nil) assert(b ~= nil) if type(a) == "number" and type(b) == "number" then if a < b then return true elseif a > b then return false else return nil end elseif type(a) == "table" and type(b) == "table" then for i = 1,math.min(#a,#b) do local result = compare(a[i],b[i]) if result ~= nil then return result end end if #a < #b then return true elseif #a > #b then return false else return nil end elseif type(a) == "number" and type(b) == "table" then return compare({a},b) elseif type(a) == "table" and type(b) == "number" then return compare(a,{b}) end end table.sort(loaded,compare) local i2, i6 for i = 1,#loaded do local function eq(n) return loaded[i] and type(loaded[i]) == "table" and #loaded[i] == 1 and type(loaded[i][1]) == "table" and #loaded[i][1] == 1 and loaded[i][1][1] == n end if eq(6) then print("Found 6 at ",i) i6 = i end if eq(2) then print("Found 2 at ",i) i2 = i end end print(i6 * i2)