fork download
  1. f = -> a {
  2. w = a.map {|s| s.split('=')}.flatten.uniq.map.with_index.to_h
  3. a.each_with_object([]) {|s, acc|
  4. x, xa, y, ya = s.split('=').map {|k| [k, acc.find {|b| b.include? k}]}.flatten(1)
  5. if xa && ya then xa.concat (acc.delete ya)
  6. elsif xa then xa << y
  7. elsif ya then ya << x
  8. else acc << [x, y]
  9. end
  10. }.map {|a| a.sort_by {|s| w[s]}}.sort_by {|a| w[a[0]]}
  11. }
  12. a = ["a1=a2", "b1=b2", "b3=b2", "c1=c2", "e1=e2",
  13. "a3=a4", "c3=c4", "e1=e3", "a2=a4", "c3=c1",
  14. "b3=a4", "c2=d1", "a4=a5", "d2=c1", "b4=b3", "d3=c3"]
  15. p f.(a)
  16.  
Success #stdin #stdout 0.01s 7984KB
stdin
Standard input is empty
stdout
[["a1", "a2", "b1", "b2", "b3", "a3", "a4", "a5", "b4"], ["c1", "c2", "c3", "c4", "d1", "d2", "d3"], ["e1", "e2", "e3"]]