feat: solve day 2 puzzle 1

This commit is contained in:
2025-12-02 11:40:10 +01:00
parent e1623ac0fa
commit fccf6464ba
6 changed files with 57 additions and 5 deletions

38
src/day02/puzzle1.lua Normal file
View 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

7
src/day02/puzzle2.lua Normal file
View File

@@ -0,0 +1,7 @@
local puzzle2 = {}
function puzzle2.solve(input)
return 0
end
return puzzle2

View File

@@ -78,14 +78,18 @@ function utils.waitForKey(targetKey)
end
end
function utils.splitLines(data)
function utils.split(data, sep)
local t = {}
for str in string.gmatch(data, "([^\n]+)") do
for str in string.gmatch(data, "([^" .. sep .. "]+)") do
table.insert(t, str)
end
return t
end
function utils.splitLines(data)
return utils.split(data, "\n")
end
function utils.round(x)
return x >= 0 and math.floor(x + 0.5) or math.ceil(x - 0.5)
end