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

View File

@@ -8,10 +8,10 @@ This project can also be run using the amazing [CraftOS-PC emulator](https://git
## Progress ## Progress
<!-- calendar-start --> <!-- calendar-start -->
#### Stars: 2/24 #### Stars: 3/24
|Mon|Tue|Wed|Thu|Fri|Sat|Sun| |Mon|Tue|Wed|Thu|Fri|Sat|Sun|
|:-:|:-:|:-:|:-:|:-:|:-:|:-:| |:-:|:-:|:-:|:-:|:-:|:-:|:-:|
|1<br>:star::star:|2<br>|3<br>|4<br>|5<br>|6<br>|7<br>| |1<br>:star::star:|2<br>:star:|3<br>|4<br>|5<br>|6<br>|7<br>|
|8<br>|9<br>|10<br>|11<br>|12<br>||| |8<br>|9<br>|10<br>|11<br>|12<br>|||
<!-- calendar-end --> <!-- calendar-end -->

3
res/examples/day02.txt Normal file
View File

@@ -0,0 +1,3 @@
11-22,95-115,998-1012,1188511880-1188511890,222220-222224,
1698522-1698528,446443-446449,38593856-38593862,565653-565659,
824824821-824824827,2121212118-2121212124

View File

@@ -4,7 +4,7 @@
"puzzle2": true "puzzle2": true
}, },
"day02": { "day02": {
"puzzle1": false, "puzzle1": true,
"puzzle2": false "puzzle2": false
}, },
"day03": { "day03": {

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
end end
function utils.splitLines(data) function utils.split(data, sep)
local t = {} local t = {}
for str in string.gmatch(data, "([^\n]+)") do for str in string.gmatch(data, "([^" .. sep .. "]+)") do
table.insert(t, str) table.insert(t, str)
end end
return t return t
end end
function utils.splitLines(data)
return utils.split(data, "\n")
end
function utils.round(x) function utils.round(x)
return x >= 0 and math.floor(x + 0.5) or math.ceil(x - 0.5) return x >= 0 and math.floor(x + 0.5) or math.ceil(x - 0.5)
end end