feat: solve day 8 puzzle 2

This commit is contained in:
2025-12-08 15:00:42 +01:00
parent 3dedc1da03
commit c516532183
3 changed files with 49 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 ## Progress
<!-- calendar-start --> <!-- calendar-start -->
#### Stars: 15/24 #### Stars: 16/24
|Mon|Tue|Wed|Thu|Fri|Sat|Sun| |Mon|Tue|Wed|Thu|Fri|Sat|Sun|
|:-:|:-:|:-:|:-:|:-:|:-:|:-:| |:-:|:-:|:-:|:-:|:-:|:-:|:-:|
|1<br>:star::star:|2<br>:star::star:|3<br>:star::star:|4<br>:star::star:|5<br>:star::star:|6<br>:star::star:|7<br>:star::star:| |1<br>:star::star:|2<br>:star::star:|3<br>:star::star:|4<br>:star::star:|5<br>:star::star:|6<br>:star::star:|7<br>:star::star:|
|8<br>:star:|9<br>|10<br>|11<br>|12<br>||| |8<br>:star::star:|9<br>|10<br>|11<br>|12<br>|||
<!-- calendar-end --> <!-- calendar-end -->

View File

@@ -29,7 +29,7 @@
}, },
"day08": { "day08": {
"puzzle1": true, "puzzle1": true,
"puzzle2": false "puzzle2": true
}, },
"day09": { "day09": {
"puzzle1": false, "puzzle1": false,

View File

@@ -1,7 +1,52 @@
local utils = require "utils"
local puzzle1 = require(SRC_PATH .. "/day08/puzzle1")
local puzzle2 = {} local puzzle2 = {}
function puzzle2.solve(input) function puzzle2.solve(input)
return 0 local boxes = {}
local lines = utils.splitLines(input)
for i, line in ipairs(lines) do
local x, y, z = line:match("(%d+),(%d+),(%d+)")
table.insert(boxes, {x=x, y=y, z=z, i=i, circuit=i})
end
local dists = {}
local circuits = {}
for i, box1 in ipairs(boxes) do
circuits[i] = i
for j=i+1, #boxes do
local box2 = boxes[j]
table.insert(dists, {
i=i,
j=j,
dist=puzzle1.dist(box1, box2)
})
end
end
table.sort(dists, function (a, b)
return a.dist < b.dist
end)
for _, d in ipairs(dists) do
local circ1 = circuits[d.i]
local circ2 = circuits[d.j]
local connex = true
for k, v in pairs(circuits) do
if v == circ2 then
circuits[k] = circ1
elseif v ~= circ1 then
connex = false
end
end
if connex then
return boxes[d.i].x * boxes[d.j].x
end
end
end end
return puzzle2 return puzzle2