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/day3.lua

28 lines
673 B
Lua
Raw 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)
2022-12-03 21:03:50 -05:00
if string.find(second, 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 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)