Thanks to visit codestin.com Credit goes to github.com
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent d86c3b4 commit 355da95Copy full SHA for 355da95
2018/08_2/ruby/leovano.rb
@@ -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
24
25
+ [res, pos + mt]
26
+end
27
28
+puts metas(input).first
0 commit comments