Compare commits
2 Commits
f5fdaf1877
...
2ce473c58d
Author | SHA1 | Date | |
---|---|---|---|
2ce473c58d | |||
93e28f068a |
49
day1/day1_part2.lua
Normal file
49
day1/day1_part2.lua
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
--Lua 5.4.2
|
||||||
|
local File = io.open("input.txt", "r")
|
||||||
|
|
||||||
|
if not File then
|
||||||
|
print("File not found")
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local LeftList = {}
|
||||||
|
local RightList = {}
|
||||||
|
|
||||||
|
for Line in File:lines() do
|
||||||
|
local LeftId, RightId = Line:match("(%d+)%s+(%d+)")
|
||||||
|
table.insert(LeftList, tonumber(LeftId))
|
||||||
|
table.insert(RightList, tonumber(RightId))
|
||||||
|
end
|
||||||
|
|
||||||
|
File:close()
|
||||||
|
|
||||||
|
if #LeftList ~= #RightList then
|
||||||
|
print("Lists are not the same size")
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
--[[
|
||||||
|
Format:
|
||||||
|
Appearances[Id] = AppearanceCount
|
||||||
|
--]]
|
||||||
|
local Appearances = {}
|
||||||
|
|
||||||
|
for ListIndex = 1, #RightList do
|
||||||
|
local Id = RightList[ListIndex]
|
||||||
|
if not Appearances[Id] then
|
||||||
|
Appearances[Id] = 0
|
||||||
|
end
|
||||||
|
Appearances[Id] = Appearances[Id] + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
local SimilarityScore = 0
|
||||||
|
|
||||||
|
for ListIndex = 1, #LeftList do
|
||||||
|
local Id = LeftList[ListIndex]
|
||||||
|
local AppearanceCount = Appearances[Id]
|
||||||
|
if AppearanceCount then
|
||||||
|
SimilarityScore = SimilarityScore + Id * AppearanceCount
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
print("Similarity score:", SimilarityScore)
|
Loading…
Reference in New Issue
Block a user