38 lines
1.2 KiB
Text
38 lines
1.2 KiB
Text
require "map"
|
|
require "table2"
|
|
local fmt = require "fmt"
|
|
|
|
local function consolidate_sets(sets)
|
|
local size = #sets
|
|
local consolidated = table.rep(size, false)
|
|
for i = 1, size - 1 do
|
|
if !consolidated[i] then
|
|
repeat
|
|
local intersects = 0
|
|
for j = i + 1, size do
|
|
if !consolidated[j] then
|
|
if !sets[i]:intersect(sets[j]):empty() then
|
|
sets[i]:merge(sets[j])
|
|
consolidated[j] = true
|
|
++intersects
|
|
end
|
|
end
|
|
end
|
|
until intersects == 0
|
|
end
|
|
end
|
|
return range(1, size):filter(|i| -> !consolidated[i]):reorder():map(|i| -> sets[i])
|
|
end
|
|
|
|
local unconsolidated_sets = {
|
|
{set.of("A", "B"), set.of("C", "D")},
|
|
{set.of("A", "B"), set.of("B", "D")},
|
|
{set.of("A", "B"), set.of("C", "D"), set.of("D", "B")},
|
|
{set.of("H", "I", "K"), set.of("A", "B"), set.of("C", "D"),
|
|
set.of("D", "B"), set.of("F", "G", "H")}
|
|
}
|
|
for unconsolidated_sets as sets do
|
|
fmt.print("Unconsolidated: %s", fmt.swrite(sets))
|
|
fmt.print("Consolidated : %s", fmt.swrite(consolidate_sets(sets)))
|
|
print()
|
|
end
|