From c5165321830b83b7b5dbb68320ffd181877d0ee1 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Mon, 8 Dec 2025 15:00:42 +0100 Subject: [PATCH] feat: solve day 8 puzzle 2 --- README.md | 4 ++-- res/stats.json | 2 +- src/day08/puzzle2.lua | 47 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index dfa4caa..4d76adb 100644 --- a/README.md +++ b/README.md @@ -8,10 +8,10 @@ This project can also be run using the amazing [CraftOS-PC emulator](https://git ## Progress -#### Stars: 15/24 +#### Stars: 16/24 |Mon|Tue|Wed|Thu|Fri|Sat|Sun| |:-:|:-:|:-:|:-:|:-:|:-:|:-:| |1
:star::star:|2
:star::star:|3
:star::star:|4
:star::star:|5
:star::star:|6
:star::star:|7
:star::star:| -|8
:star:|9
|10
|11
|12
||| +|8
:star::star:|9
|10
|11
|12
||| diff --git a/res/stats.json b/res/stats.json index 86b93f0..d79cd4d 100644 --- a/res/stats.json +++ b/res/stats.json @@ -29,7 +29,7 @@ }, "day08": { "puzzle1": true, - "puzzle2": false + "puzzle2": true }, "day09": { "puzzle1": false, diff --git a/src/day08/puzzle2.lua b/src/day08/puzzle2.lua index 2f4bc9a..91238e9 100644 --- a/src/day08/puzzle2.lua +++ b/src/day08/puzzle2.lua @@ -1,7 +1,52 @@ +local utils = require "utils" +local puzzle1 = require(SRC_PATH .. "/day08/puzzle1") local puzzle2 = {} 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 return puzzle2