Cleaned up Day 3

This commit is contained in:
Logan G 2022-12-03 00:45:41 -07:00
parent dd53bb6cde
commit b76699fca6
Signed by: logan
GPG key ID: E328528C921E7A7A
2 changed files with 23 additions and 29 deletions

View file

@ -3,30 +3,25 @@ io.input(file)
score = 0
for line in file:lines() do
local first = string.sub(line, 1, #line/2)
local second = string.sub(line, #line/2 + 1, #line)
print(#first)
print(#second)
function find_duplicates(first, second)
for i = 1, #first do
local char = string.sub(first, i, i)
if string.match(second, char) then
print(char)
if char == string.upper(char) then
--print(string.byte(char) - string.byte("A") + 27)
score = score + string.byte(char) - string.byte("A") + 27
goto continue
return string.byte(char) - string.byte("A") + 27
elseif char == string.lower(char) then
--print(string.byte(char) - string.byte("a"))
score = score + string.byte(char) - string.byte("a") + 1
goto continue
return string.byte(char) - string.byte("a") + 1
end
end
end
end
end
for line in file:lines() do
local first = string.sub(line, 1, #line/2)
local second = string.sub(line, #line/2 + 1, #line)
::continue::
score = score + find_duplicates(first, second)
end
print(score)

View file

@ -8,26 +8,25 @@ for line in file:lines() do
table.insert(data, line)
end
function find_duplicates(one, two, three)
for i = 1, #one do
local char = string.sub(one, i, i)
if string.match(two, char) and string.match(three, char) then
if char == string.upper(char) then
return string.byte(char) - string.byte("A") + 27
elseif char == string.lower(char) then
return string.byte(char) - string.byte("a") + 1
end
end
end
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]
for n = 1, #one do
local char = string.sub(one, n, n)
if string.match(two, char) and string.match(three, char) then
if char == string.upper(char) then
score = score + string.byte(char) - string.byte("A") + 27
goto continue
elseif char == string.lower(char) then
score = score + string.byte(char) - string.byte("a") + 1
goto continue
end
end
end
::continue::
score = score + find_duplicates(one, two, three)
end
print(score)