42 lines
788 B
Lua
42 lines
788 B
Lua
|
function find_same(first, second)
|
||
|
for c in first:gmatch('.') do
|
||
|
if string.find(second, 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, sack in pairs(sacks) do
|
||
|
local mid = math.floor(string.len(sack) / 2)
|
||
|
local first = string.sub(sack, 0, mid)
|
||
|
local second = string.sub(sack, mid + 1)
|
||
|
|
||
|
local same = find_same(first, second)
|
||
|
total = total + priority(same)
|
||
|
print(same, total)
|
||
|
end
|
||
|
|
||
|
print(total)
|
||
|
|
||
|
|