Post by Mercobots on Nov 28, 2018 2:20:35 GMT
I think is that one
-- ++++++++++++++++++++++++++++++++++++++++++++
function table_merge(t1, t2)
for _, v in ipairs(t2) do
table.insert(t1, v)
end
end
-- ++++++++++++++++++++++++++++++++++++++++++++
function getMaster(tbl, rules)
local result = false
for _, rule in ipairs(rules) do
for i, v in ipairs(tbl) do
result = v
if tostring(v) ~= tostring(rule) then
result = false
break
end
end
if result then break end
end
return result
end
-- ++++++++++++++++++++++++++++++++++++++++++++
function validateTable(data)
for row, tbl in ipairs(data) do
if typeOf(tbl) ~= 'table' then
print("Row:" .. row .. " Should be a table instead of " .. typeOf(tbl))
scriptExit()
end
for col, val in ipairs(tbl) do
if typeOf(val) == 'number' then
val = tostring(val)
elseif typeOf(val) ~= 'string' then
print("Value form [row:" .. row .. " - col:" .. col .. "] should be string or int instead of " .. typeOf(val))
scriptExit()
elseif string.len(val) <= 0 then
print("Value form [row:" .. row .. " - col:" .. col .. "] is empty")
scriptExit()
end
end
end
return true
end
-- ++++++++++++++++++++++++++++++++++++++++++++
function start(data, rules)
validateTable(data)
local master_key, master_val
local _temp, continue = {}, true
for i, tbl in ipairs(data) do
local master = getMaster(tbl, rules)
if master and master ~= master_val then
continue = true
end
if continue then
if master then
master_key = i
master_val = master
elseif tbl[#tbl] == master_val then
tbl[#tbl] = nil
table.insert(_temp[master_key], master_val)
elseif master_key then
continue = false
end
end
_temp[i] = tbl
end
local result = {}
for i, tbl in ipairs(_temp) do
table_merge(result, tbl)
end
return table.concat(result, "")
end
-- RULES
-- ++++++++++++++++++++++++++++++++++++++++++++
local rules = { 0, 1 }
-- ++++++++++++++++++++++++++++++++++++++++++++
-- TEST 1
-- ++++++++++++++++++++++++++++++++++++++++++++
local data = {
{ 0, 0, 0, 0, 0, 0 },
{ 8, 8, 8, 8, 8, 0 },
{ 0, 0, 0, 8, 8, 8 },
{ 1, 1, 1, 1, 8, 0 },
{ 8, 8, 8, 8, 8, 0 },
}
print(start(data, rules))
-- ++++++++++++++++++++++++++++++++++++++++++++
-- TEST 2
-- ++++++++++++++++++++++++++++++++++++++++++++
local data = {
{ 0, 0, 0, 0, 0, 0 },
{ 8, 8, 8, 8, 8, 0 },
{ 0, 0, 0, 8, 8, 8 },
{ 1, 1, 1, 1, 1, 1 },
{ 8, 8, 8, 8, 8, 1 },
}
print(start(data, rules))