summaryrefslogtreecommitdiff
path: root/13/1.lua
blob: 94d8bd984b582756f4cebe9703535e66c63c2161 (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
local lines = {}
for line in io.lines() do table.insert(lines,line) end

local sum = 0
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)()
	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
	if compare(l1,l2) then
		sum = sum + ((i+2)/3)
	end
end
print(sum)