This repository has been archived on 2023-12-02. You can view files and clone it, but cannot push or open issues or pull requests.
logan_2022/day3/day3part2.lua

32 lines
759 B
Lua

file = io.open("input")
io.input(file)
score = 0
data = {}
for line in file:lines() do
table.insert(data, line)
end
function find_duplicates(one, two, three)
for i = 1, #one do
local char = string.sub(one, i, i)
if string.find(two, char) and string.find(three, char) then
if char == string.upper(char) then
return string.byte(char) - string.byte("A") + 27
elseif char == string.lower(char) then
return string.byte(char) - string.byte("a") + 1
end
end
end
end
for i = 1, #data/3 do
local one = data[(i-1)*3+1]
local two = data[(i-1)*3+2]
local three = data[(i-1)*3+3]
score = score + find_duplicates(one, two, three)
end
print(score)