You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
668 B
35 lines
668 B
raw = ARGF.read.chomp.split(/\s+/).map(&:to_i)
|
|
|
|
class Node
|
|
def self.from(raw)
|
|
child_count = raw.shift
|
|
metadata_count = raw.shift
|
|
|
|
children = child_count.times.map { Node.from(raw) }
|
|
metadata = metadata_count.times.map { raw.shift }
|
|
|
|
Node.new(children, metadata)
|
|
end
|
|
|
|
def initialize(children, metadata)
|
|
@children, @metadata = children, metadata
|
|
end
|
|
|
|
def checksum
|
|
@metadata.sum + @children.map(&:checksum).sum
|
|
end
|
|
|
|
def value
|
|
if @children.empty?
|
|
@metadata.sum
|
|
else
|
|
@metadata.flat_map {|index|
|
|
@children.fetch(index-1, [])
|
|
}.map(&:value).sum
|
|
end
|
|
end
|
|
end
|
|
|
|
root = Node.from(raw)
|
|
p root.value
|