function find_same(first, second, third) for c in first:gmatch('.') do if string.find(second, c) ~= nil and string.find(third, c) ~= nil then return c end end end function priority(char) dec = string.byte(char) if dec & 0x20 == 0 then -- Uppercase return dec - string.byte('A') + 27 else -- Lowercase return dec - string.byte('a') + 1 end end file = io.open('input') sacks = {} for line in file:lines() do table.insert(sacks, line) end total = 0 for i = 1, math.floor(#sacks / 3) do local i0 = i - 1 local first = sacks[i0 * 3 + 1] local second = sacks[i0 * 3 + 2] local third = sacks[i0 * 3 + 3] local same = find_same(first, second, third) total = total + priority(same) print(same, total) end print(total)