feat: solve day 2 puzzle 1
This commit is contained in:
38
src/day02/puzzle1.lua
Normal file
38
src/day02/puzzle1.lua
Normal file
@@ -0,0 +1,38 @@
|
||||
local utils = require "utils"
|
||||
local puzzle1 = {}
|
||||
|
||||
function puzzle1.splitRange(range)
|
||||
local parts = utils.split(range, "-")
|
||||
return tonumber(parts[1]), tonumber(parts[2])
|
||||
end
|
||||
|
||||
function puzzle1.isValid(id)
|
||||
local str = tostring(id)
|
||||
if #str % 2 == 1 then
|
||||
return true
|
||||
end
|
||||
local mid = #str / 2
|
||||
return str:sub(1, mid) ~= str:sub(mid + 1)
|
||||
end
|
||||
|
||||
function puzzle1.countInvalids(range)
|
||||
local min, max = puzzle1.splitRange(range)
|
||||
local total = 0
|
||||
for i=min, max do
|
||||
if not puzzle1.isValid(i) then
|
||||
total = total + i
|
||||
end
|
||||
end
|
||||
return total
|
||||
end
|
||||
|
||||
function puzzle1.solve(input)
|
||||
local ranges = utils.split(input, ",")
|
||||
local total = 0
|
||||
for _, range in ipairs(ranges) do
|
||||
total = total + puzzle1.countInvalids(range)
|
||||
end
|
||||
return total
|
||||
end
|
||||
|
||||
return puzzle1
|
||||
Reference in New Issue
Block a user