summaryrefslogtreecommitdiff
path: root/13/2.lua
blob: 4af213c0bca3de919e3661874bba4f35321d5ba2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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)