Files
adventofcode/2025/Day7/Day7.lua

81 lines
1.8 KiB
Lua

-- Day 7 - Advent of Code 2025 - Bas Grolleman
require("functions")
local input_file = "Day7/test"
local input = file_characters_to_table(input_file)
local rows = tablelength(input)
local cols = tablelength(input[1])
local splits = 0
local quantumsplits = 0
print("Rows " .. rows .. " Cols " .. cols)
print(table.concat(input[1]))
for R = 2, rows do
for C = 1, cols do
if input[R][C] == "." then
if input[R - 1][C] == "S" or input[R - 1][C] == "|" then
input[R][C] = "|"
end
elseif input[R][C] == "^" and input[R - 1][C] == "|" then
if input[R][C - 1] ~= "|" then
input[R][C - 1] = "|"
end
if input[R][C + 1] ~= "|" then
quantumsplits = quantumsplits + 2
input[R][C + 1] = "|"
end
splits = splits + 1
end
end
print(table.concat(input[R]) .. " " .. quantumsplits)
end
print("Total splits " .. splits)
print("Total quantumsplits " .. quantumsplits)
-- Okay, time to learn how to make a tree node
-- Great, I learned how to try a brute force (that will obviously never work)
function createNode(col)
local node = {
col = col,
childeren = {},
}
return node
end
local root = createNode(math.ceil(cols / 2))
local active_nodes = { root }
for row = 2, rows do
for k, n in ipairs(active_nodes) do
if next(n.childeren) == nil then
if input[row][n.col] == "^" then
local a = createNode(n.col - 1)
local b = createNode(n.col + 1)
table.insert(active_nodes, a)
table.insert(active_nodes, b)
n.childeren = { a, b }
end
end
end
end
local gt = 0
function count_table(t, indent)
indent = indent or ""
print(indent .. "c" .. t.col)
if next(t.childeren) ~= nil then
for k, v in pairs(t.childeren) do
count_table(v, indent .. " ")
end
else
gt = gt + 1
end
end
count_table(root)
print("Grand Total " .. gt)