Skip to content

Commit

Permalink
[Day 01] Refactor solution to OOP
Browse files Browse the repository at this point in the history
  • Loading branch information
nihas101 committed Dec 1, 2024
1 parent 0c52f19 commit 9555aee
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 22 deletions.
97 changes: 76 additions & 21 deletions day01.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,91 @@ local split_str = require('lib/split_str')
local read_input = require('lib/read_input')
table.zip = require('lib/table_zip')

local function part1(xs, ys)
local id_pairs = table.zip(xs, ys)
local Day01 = {
mapfun = nil,
redfun = nil,
solve = nil
}
function Day01:new(o)
o = o or {}

local distances = {}
for _, pair in pairs(id_pairs) do
table.insert(distances, math.abs(pair[1] - pair[2]))
o.parent = self
setmetatable(o, self)
self.__index = self

return o
end

function Day01:map(source, target)
for _, v in pairs(source) do
self.mapfun(target, v)
end
return target
end

local distance_sum = 0
for _, e in pairs(distances) do
distance_sum = distance_sum + e
function Day01:reduce(source, target)
for _, v in pairs(source) do
target = self.redfun(target, v)
end
return distance_sum
return target
end

local Part1 = Day01:new()

function Part1:new(xs, ys)
local o = {
mapfun = function(distances, pair)
return table.insert(distances, math.abs(pair[1] - pair[2]))
end,
redfun = function(distance_sum, distance)
return distance_sum + distance
end
}
Day01.new(self, o)

o.xs = xs
o.ys = ys

return o
end

local function part2(ids, frequency_ids)
function Part1:solve()
local id_pairs = table.zip(self.xs, self.ys)
local distances = self:map(id_pairs, {})
return self:reduce(distances, 0)
end

local Part2 = Day01:new()

function Part2:new(ids, frequency_ids)
local frequencies = {}
setmetatable(frequencies, {
__index = function(_, _)
return 0
end
})
local o = {
mapfun = function(_, freq)
frequencies[freq] = frequencies[freq] + 1
return frequencies
end,
redfun = function(similarity_score, e)
similarity_score = similarity_score + (e * frequencies[e])
return similarity_score
end
}
Day01.new(self, o)

for _, freq in pairs(frequency_ids) do
frequencies[freq] = frequencies[freq] + 1
end
o.ids = ids
o.frequency_ids = frequency_ids
o.frequencies = frequencies

local similarity_score = 0
for _, e in pairs(ids) do
local mult = frequencies[e]
similarity_score = similarity_score + (e * mult)
end
return similarity_score
return o
end

function Part2:solve()
self:map(self.frequency_ids, self.frequencies)
return self:reduce(self.ids, 0)
end

local input = read_input("3 4\
Expand All @@ -57,5 +109,8 @@ end
table.sort(left_ids)
table.sort(right_ids)

print("Part 1: " .. part1(left_ids, right_ids))
print("Part 2: " .. part2(left_ids, right_ids))
local p1 = Part1:new(left_ids, right_ids)
print("Part 1: " .. p1:solve())

local p2 = Part2:new(left_ids, right_ids)
print("Part 2: " .. p2:solve())
4 changes: 3 additions & 1 deletion lib/read_file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ local function read_file(file)
if not f then
return nil
end
return f:read "*a"
local content = f:read "*a"
f:close()
return content
end

return read_file

0 comments on commit 9555aee

Please sign in to comment.