aoc2022/day3/init.lua
2022-12-03 20:57:12 +01:00

32 lines
771 B
Lua

local file = io.open("input.txt")
local contents = file:read("*l")
local priority = 0
function table.contains(table, element)
for _, value in pairs(table) do
if value == element then
return true
end
end
return false
end
while contents do
local len = string.len(contents)
local comp1 = {}
string.gsub(string.sub(contents, 1, len / 2 + 1), ".",function(c) table.insert(comp1,c) end)
local comp2 = {}
string.gsub(string.sub(contents, len / 2 + 1, len), ".",function(c) table.insert(comp2,c) end)
for _, v in ipairs(comp1) do
if table.contains(comp2, v) then
priority = priority + string.byte(v)
break
end
end
contents = file:read("*l")
end
print(priority)