Skip to content

Commit

Permalink
[Day01] Simplify solution for part 2 via metatable
Browse files Browse the repository at this point in the history
  • Loading branch information
nihas101 committed Dec 1, 2024
1 parent 49e38b1 commit 0c52f19
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions day01.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,19 @@ end

local function part2(ids, frequency_ids)
local frequencies = {}
local prev_element, prev_frequency = frequency_ids[1], 1
for i = 2, #frequency_ids do
if prev_element == frequency_ids[i] then
prev_frequency = prev_frequency + 1
else
frequencies[prev_element] = prev_frequency
prev_element, prev_frequency = frequency_ids[i], 1
setmetatable(frequencies, {
__index = function(_, _)
return 0
end
})

for _, freq in pairs(frequency_ids) do
frequencies[freq] = frequencies[freq] + 1
end
frequencies[prev_element] = prev_frequency

local similarity_score = 0
for _, e in pairs(ids) do
local mult = frequencies[e]
if mult == nil then
mult = 0
end
similarity_score = similarity_score + (e * mult)
end
return similarity_score
Expand Down

0 comments on commit 0c52f19

Please sign in to comment.