Forgot to commit the last few days
This commit is contained in:
121
2025/Day8/Day8.lua
Normal file
121
2025/Day8/Day8.lua
Normal file
@@ -0,0 +1,121 @@
|
||||
-- Advent of Code 2025 - Day 8 - Bas Grolleman
|
||||
require("functions")
|
||||
|
||||
-- Variables
|
||||
local boxes = {}
|
||||
local inputfile = "Day8/test"
|
||||
local limit = 11
|
||||
|
||||
function distance(p1, p2)
|
||||
return math.sqrt((p2.x - p1.x) ^ 2 + (p2.y - p1.y) ^ 2 + (p2.z - p1.z) ^ 2)
|
||||
end
|
||||
function boxes.find(id)
|
||||
for i, box in ipairs(boxes) do
|
||||
if box.id == id then
|
||||
return box
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local id_counter = 0
|
||||
for line in io.lines(inputfile) do
|
||||
local x, y, z = line:match("(%d+)%,(%d+)%,(%d+)")
|
||||
id_counter = id_counter + 1
|
||||
local box = {
|
||||
id = id_counter,
|
||||
x = tonumber(x),
|
||||
y = tonumber(y),
|
||||
z = tonumber(z),
|
||||
xyz = x .. "," .. y .. "," .. z,
|
||||
}
|
||||
table.insert(boxes, box)
|
||||
end
|
||||
|
||||
local distance_table = {}
|
||||
for i, box1 in ipairs(boxes) do
|
||||
for j, box2 in ipairs(boxes) do
|
||||
if box1.id ~= box2.id then
|
||||
table.insert(distance_table, {
|
||||
id1 = box1.id,
|
||||
id2 = box2.id,
|
||||
d = distance(box1, box2),
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
table.sort(distance_table, function(a, b)
|
||||
return a.d < b.d
|
||||
end)
|
||||
|
||||
local circuit = 0
|
||||
for i, dt in ipairs(distance_table) do
|
||||
box1 = boxes.find(dt.id1)
|
||||
box2 = boxes.find(dt.id2)
|
||||
if limit > 0 then
|
||||
io.write(string.format("%2d Linking %2d <-> %2d %.0f", limit, box1.id, box2.id, dt.d))
|
||||
if box1.c == nil and box2.c == nil then
|
||||
circuit = circuit + 1
|
||||
box1.c = circuit
|
||||
box2.c = circuit
|
||||
limit = limit - 1
|
||||
print(" C=" .. circuit .. " BOTH")
|
||||
elseif box1.c ~= nil and box2.c ~= nil then
|
||||
if box1.c == box2.c then
|
||||
print(" C=" .. box1.c .. " ALREADY")
|
||||
else
|
||||
-- Oh look, we merge 2 circuits!
|
||||
print(" Merging " .. box1.c .. " and " .. box2.c)
|
||||
local remove = box2.c
|
||||
for k, uc in ipairs(boxes) do
|
||||
if uc.c == remove then
|
||||
uc.c = box1.c
|
||||
end
|
||||
end
|
||||
limit = limit - 1
|
||||
end
|
||||
elseif box1.c == nil then
|
||||
box1.c = box2.c
|
||||
limit = limit - 1
|
||||
print(" C=" .. box1.c .. " HOOK BOX1")
|
||||
elseif box2.c == nil then
|
||||
box2.c = box1.c
|
||||
limit = limit - 1
|
||||
print(" C=" .. box1.c .. " HOOK BOX2")
|
||||
end
|
||||
end
|
||||
end
|
||||
for k, box in ipairs(boxes) do
|
||||
if box.c == nil then
|
||||
circuit = circuit + 1
|
||||
box.c = circuit
|
||||
end
|
||||
end
|
||||
table.sort(boxes, function(a, b)
|
||||
return a.c < b.c
|
||||
end)
|
||||
|
||||
local lc = 0
|
||||
local circuit_size = {}
|
||||
|
||||
for k, box in ipairs(boxes) do
|
||||
if lc ~= box.c then
|
||||
circuit_size[box.c] = 1
|
||||
print()
|
||||
else
|
||||
circuit_size[box.c] = circuit_size[box.c] + 1
|
||||
end
|
||||
print(string.format("Box %2d Circuit %2d", box.id, box.c))
|
||||
lc = box.c
|
||||
end
|
||||
|
||||
print()
|
||||
local circuit_size2 = {}
|
||||
for k, v in pairs(circuit_size) do
|
||||
circuit_size2[#circuit_size2 + 1] = v
|
||||
end
|
||||
table.sort(circuit_size2, function(a, b)
|
||||
return b < a
|
||||
end)
|
||||
print_table(circuit_size2)
|
||||
local total = circuit_size2[1] * circuit_size2[2] * circuit_size2[3]
|
||||
print("Total size " .. total)
|
||||
Reference in New Issue
Block a user