Search code examples
lualua-table

Lua arrays print results in specified order


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 start(data, rules)
    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}

local data = {
    { 0, 0, 0, 0, 0, 0 },
    { 1, 1, 1, 1, 1, 0 },
    { 0, 0, 0, 8, 1, 0 },
    { 1, 1, 1, 1, 8, 8 },
    { 0, 0, 0, 0, 0, 0 },
}

start(data, rules)

OUTPUT:

000000001111100081111188000000

The expected results should be this:

000000001111110008111188000000

How do I achieve the required results? the table can contain more than six elements but initially it is six. so when all the elements are 1, it will check the next immediate Table to see if the last element is also one; if true it removes it and adds to the six making seven, it will then check again for the next immediate table if the last digit is 1, if true it removes that also and adds to the seven making 8. It will check again and if false return to where it left. So here the table whose element were removed become five in number. So I was hoping that they could satisfy the rule also if all the five elemnts are 1 or 0. But it seems only the six elements satisfy the rule...

I hope it's clear Thanks


Solution

  • function table_merge(t1, t2)
       for _, v in ipairs(t2) do
          table.insert(t1, v)
       end
    end
    
    function getMaster(tbl, rules, w)
       local result = false
       for _, rule in ipairs(rules) do
          for i = 1, w do
             local v = tbl[i]
             result = v
             if tostring(v) ~= tostring(rule) then
                result = false
                break
             end
          end
          if result then break end
       end
       return result
    end
    
    function start(data, rules)
       local width = #data[1]  -- assuming all data rows have the same width
       local master_keys = {}
       local master_values = {}
       local continue_idx = width + 1
    
       for i, tbl in ipairs(data) do
          for w = width, 1, -1 do
             if w >= continue_idx and tbl[w] == master_values[w] then
                table.insert(data[master_keys[w]], master_values[w])
                tbl[w] = nil
             else
                local master = getMaster(tbl, rules, w)
                if master then
                   master_keys[w] = i
                   master_values[w] = master
                   continue_idx = w
                else
                   continue_idx = w + 1
                end
                break
             end
          end
       end
    
       local result = {}
       for i, tbl in ipairs(data) do
          table_merge(result, tbl)
       end
       return table.concat(result, "")
    end
    
    -- RULES
    local rules = { 0, 1 }
    
    local data = {
       { 0, 0, 0, 0, 0, 0 },
       { 1, 1, 1, 1, 1, 0 },
       { 0, 0, 0, 8, 1, 0 },
       { 1, 1, 1, 1, 8, 8 },
       { 0, 0, 0, 0, 0, 0 },
    }
    
    print(start(data, rules))