Created
June 13, 2021 21:11
-
-
Save seanstickle/83925a26013ee2f1622c2ce16b74ec2c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "csv" | |
input = File.readlines(ARGV[0], chomp: true).map do |line| | |
line.split(/,/) | |
end | |
unique_elements = input.flatten.uniq.size | |
def merge(input) | |
data = input.clone | |
candidate = data.shift | |
@match = false | |
data.each_with_index do |line, i| | |
if (candidate & line).count > 0 | |
data[i] = (candidate + line).uniq.sort | |
@match = true | |
break | |
end | |
end | |
if @match == false | |
data << candidate | |
end | |
data | |
end | |
while true | |
output = merge(input) | |
if unique_elements == input.flatten.size | |
break | |
end | |
input = output | |
end | |
CSV do |csv| | |
output.each do |line| | |
csv << line | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment