feat: solve day 2 puzzle 2

This commit is contained in:
2025-12-02 11:53:33 +01:00
parent fccf6464ba
commit a08eaf0007
3 changed files with 43 additions and 4 deletions

View File

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

View File

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

View File

@@ -1,7 +1,46 @@
local utils = require "utils"
local puzzle1 = require(SRC_PATH .. "/day02/puzzle1")
local puzzle2 = {}
function puzzle2.isValid(id)
local str = tostring(id)
local totLen = #str
for len=1, totLen - 1 do
if totLen % len == 0 then
local ref = str:sub(1, len)
local allSame = true
for i=1, totLen / len - 1 do
if str:sub(len * i + 1, len * (i + 1)) ~= ref then
allSame = false
break
end
end
if allSame then
return false
end
end
end
return true
end
function puzzle2.countInvalids(range)
local min, max = puzzle1.splitRange(range)
local total = 0
for i=min, max do
if not puzzle2.isValid(i) then
total = total + i
end
end
return total
end
function puzzle2.solve(input)
return 0
local ranges = utils.split(input, ",")
local total = 0
for _, range in ipairs(ranges) do
total = total + puzzle2.countInvalids(range)
end
return total
end
return puzzle2