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

42 lines
788 B
Lua
Raw Normal View History

2022-12-03 00:37:21 -05:00
function find_same(first, second)
for c in first:gmatch('.') do
if string.find(second, 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, sack in pairs(sacks) do
local mid = math.floor(string.len(sack) / 2)
local first = string.sub(sack, 0, mid)
local second = string.sub(sack, mid + 1)
local same = find_same(first, second)
total = total + priority(same)
print(same, total)
end
print(total)