Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 355da95

Browse files
author
Leo Silva Souza
committed
08/2 - Ruby
1 parent d86c3b4 commit 355da95

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

2018/08_2/ruby/leovano.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
raise 'Arg1' if ARGV[0].nil?
4+
5+
input = File.read(ARGV[0]).split.map(&:to_i)
6+
7+
def metas(tree)
8+
ch, mt = tree.take 2
9+
10+
pos = 2
11+
res = nil
12+
13+
if ch.zero?
14+
res = tree[pos..-1].take(mt).sum
15+
else
16+
childs = Array.new(ch).map do
17+
child = metas tree[pos..-1]
18+
pos += child[1]
19+
child[0]
20+
end
21+
22+
res = tree[pos..-1].take(mt).map { |it| childs[it - 1] || 0 }.sum
23+
end
24+
25+
[res, pos + mt]
26+
end
27+
28+
puts metas(input).first

0 commit comments

Comments
 (0)