adventofcode2022/day3/day3.lua

28 lines
674 B
Lua
Raw Permalink Normal View History

2022-12-03 01:05:08 -05:00
file = io.open("input")
io.input(file)
score = 0
2022-12-03 02:45:41 -05:00
function find_duplicates(first, second)
2022-12-03 01:05:08 -05:00
for i = 1, #first do
local char = string.sub(first, i, i)
if string.match(second, char) then
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 line in file:lines() do
local first = string.sub(line, 1, #line/2)
local second = string.sub(line, #line/2 + 1, #line)
2022-12-03 01:05:08 -05:00
2022-12-03 02:45:41 -05:00
score = score + find_duplicates(first, second)
2022-12-03 01:05:08 -05:00
end
print(score)