adventofcode2022/day3/day3part2.lua

33 lines
759 B
Lua
Raw Normal View History

2022-12-03 01:05:08 -05:00
file = io.open("input")
io.input(file)
score = 0
data = {}
for line in file:lines() do
table.insert(data, line)
end
2022-12-03 02:45:41 -05:00
function find_duplicates(one, two, three)
for i = 1, #one do
local char = string.sub(one, i, i)
2022-12-03 21:03:50 -05:00
if string.find(two, char) and string.find(three, char) then
2022-12-03 01:05:08 -05:00
if char == string.upper(char) then
2022-12-03 02:45:41 -05:00
return string.byte(char) - string.byte("A") + 27
2022-12-03 01:05:08 -05:00
elseif char == string.lower(char) then
2022-12-03 02:45:41 -05:00
return string.byte(char) - string.byte("a") + 1
2022-12-03 01:05:08 -05:00
end
end
end
2022-12-03 02:45:41 -05:00
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]
2022-12-03 01:05:08 -05:00
2022-12-03 02:45:41 -05:00
score = score + find_duplicates(one, two, three)
2022-12-03 01:05:08 -05:00
end
print(score)