blob: 2ef3505e0b5d52e5a2789594e6e708dcf0d9634f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
#!/usr/bin/env ruby
require 'pp'
class Array
def to_empty_hash
h = {}
self.each do |el|
h[el] = nil
end
return h
end
end
input = $stdin.readlines.map(&:chomp)
hash = {}
input.each do |line|
me, them = line.split(" -> ")
_, name, weight = me.match(/([a-z]+) \((\d+)\)/).to_a
children = them.nil? ? [] : them.split(", ")
hash[name.intern] = {
:weight => weight,
:remove => false,
:children => children.map(&:intern).to_empty_hash
}
end
hash.each do |key, value|
value[:children].each do |ckey, cvalue|
if cvalue.nil? then
value[:children][ckey] = hash[ckey].dup
hash[ckey][:remove] = true
end
end
end
hash = hash.reject { |key, value|
value[:remove]
}
puts hash.keys.first
|