53 lines
962 B
Lua
53 lines
962 B
Lua
io.input("test")
|
|
io.input("input")
|
|
|
|
function getPriority(c)
|
|
if c>='a' and c<='z' then
|
|
return string.byte(c) - string.byte('a') + 1
|
|
else
|
|
return string.byte(c) - string.byte('A') + 27
|
|
end
|
|
end
|
|
|
|
-- for part 1
|
|
local sum = 0
|
|
local linecount = 0
|
|
-- for part 2
|
|
local common = {}
|
|
local sumBadges = 0
|
|
|
|
for line in io.lines() do
|
|
local h = {}
|
|
local i = 0
|
|
local repeated = nil
|
|
for c in string.gmatch(line, "%a") do
|
|
-- part 1
|
|
if i<#line/2 then
|
|
h[c] = (h[c] or 0) + 1
|
|
elseif not repeated and h[c] then
|
|
repeated = c
|
|
end
|
|
i = i + 1
|
|
|
|
-- part 2
|
|
if linecount==0 then
|
|
common[c] = 1
|
|
elseif common[c] and linecount==1 then
|
|
common[c] = 2
|
|
elseif common[c]==2 then -- 2
|
|
common[c] = 3
|
|
sumBadges = sumBadges + getPriority(c)
|
|
end
|
|
end
|
|
-- part 1
|
|
local priority = getPriority(repeated)
|
|
sum = sum + priority
|
|
|
|
-- part 2
|
|
if linecount==2 then common = {} end
|
|
linecount = (linecount + 1)%3
|
|
end
|
|
|
|
print("part 1", sum)
|
|
print("part 2", sumBadges)
|