This repository has been archived on 2023-12-03. You can view files and clone it, but cannot push or open issues or pull requests.
thomas_2022/day_3/main2.lua

43 lines
836 B
Lua
Raw Normal View History

2022-12-03 00:37:21 -05:00
function find_same(first, second, third)
for c in first:gmatch('.') do
if string.find(second, c) ~= nil and string.find(third, c) ~= nil then
return c
end
end
end
function priority(char)
dec = string.byte(char)
if dec & 0x20 == 0 then
-- Uppercase
return dec - string.byte('A') + 27
else
-- Lowercase
return dec - string.byte('a') + 1
end
end
file = io.open('input')
sacks = {}
for line in file:lines() do
table.insert(sacks, line)
end
total = 0
for i = 1, math.floor(#sacks / 3) do
local i0 = i - 1
local first = sacks[i0 * 3 + 1]
local second = sacks[i0 * 3 + 2]
local third = sacks[i0 * 3 + 3]
local same = find_same(first, second, third)
total = total + priority(same)
print(same, total)
end
print(total)