Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function checkRangesFull(range1,range2)
- local r1l = ""
- local r1u = ""
- local r2l = ""
- local r2u = ""
- local r1r = 0
- local r2r = 0
- local delimit = false
- local fullMatch = true
- for c=1, #range1 do
- if range1:sub(c,c) ~= "-" and not delimit then r1l = r1l..range1:sub(c,c)
- elseif range1:sub(c,c) == "-" then delimit = true
- elseif range1:sub(c,c) ~= "-" and delimit then r1u = r1u..range1:sub(c,c) else print("this shouldn't be running") end
- end
- delimit = false
- for c=1, #range2 do
- if range2:sub(c,c) ~= "-" and not delimit then r2l = r2l..range2:sub(c,c)
- elseif range2:sub(c,c) == "-" then delimit = true
- elseif range2:sub(c,c) ~= "-" and delimit then r2u = r2u..range2:sub(c,c) else print("this shouldn't be running") end
- end
- r1r = tonumber(r1u) - tonumber(r1l)
- r2r = tonumber(r2u) - tonumber(r2l)
- local r1t = {}
- local r2t = {}
- for n=0, r1r do
- table.insert(r1t,tonumber(r1l)+n)
- end
- for n=0, r2r do
- table.insert(r2t,tonumber(r2l)+n)
- end
- if r1r-r2r<=0 then
- for k, v in pairs(r1t) do
- if fullMatch then
- if not table.contains(r2t,v) then fullMatch = false end
- end
- end
- end
- if r2r-r1r<=0 then
- for k, v in pairs(r2t) do
- if fullMatch then
- if not table.contains(r1t,v) then fullMatch = false end
- end
- end
- end
- return fullMatch
- end
- function checkRangesPartial(range1,range2)
- local r1l = ""
- local r1u = ""
- local r2l = ""
- local r2u = ""
- local r1r = 0
- local r2r = 0
- local delimit = false
- local anyMatch = false
- print(range1.." "..range2)
- for c=1, #range1 do
- if range1:sub(c,c) ~= "-" and not delimit then r1l = r1l..range1:sub(c,c)
- elseif range1:sub(c,c) == "-" then delimit = true
- elseif range1:sub(c,c) ~= "-" and delimit then r1u = r1u..range1:sub(c,c) else print("this shouldn't be running") end
- end
- delimit = false
- for c=1, #range2 do
- if range2:sub(c,c) ~= "-" and not delimit then r2l = r2l..range2:sub(c,c)
- elseif range2:sub(c,c) == "-" then delimit = true
- elseif range2:sub(c,c) ~= "-" and delimit then r2u = r2u..range2:sub(c,c) else print("this shouldn't be running") end
- end
- r1r = tonumber(r1u) - tonumber(r1l)
- r2r = tonumber(r2u) - tonumber(r2l)
- local r1t = {}
- local r2t = {}
- for n=0, r1r do
- table.insert(r1t,tonumber(r1l)+n)
- end
- for n=0, r2r do
- table.insert(r2t,tonumber(r2l)+n)
- end
- for k, v in pairs(r1t) do
- if not anyMatch then
- if table.contains(r2t,v) then anyMatch = true print("a match was found, exiting... "..v) end
- end
- end
- for k, v in pairs(r2t) do
- if not anyMatch then
- if table.contains(r1t,v) then anyMatch = true print("a match was found, exiting... "..v) end
- end
- end
- return anyMatch
- end
- function table.contains(table, element)
- for _, e in pairs(table) do
- if e == element then
- return true
- end
- end
- end
- local tasks = io.open("input","r")
- local fullMatches = 0
- local anyMatches = 0
- for sets in io.lines("input") do
- local range1 = ""
- local range2 = ""
- local delimit = false
- local temp = tasks:read()
- for c=1, #temp do
- if temp:sub(c,c) ~= "," and not delimit then range1 = range1..temp:sub(c,c)
- elseif temp:sub(c,c) == "," then delimit = true
- elseif temp:sub(c,c) ~= "," and delimit then range2 = range2..temp:sub(c,c) end
- end
- if checkRangesFull(range1,range2) then fullMatches = fullMatches + 1 end
- if checkRangesPartial(range1,range2) then anyMatches = anyMatches + 1 end
- end
- print(fullMatches)
- print(anyMatches)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement