-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.rb
executable file
·57 lines (40 loc) · 980 Bytes
/
main.rb
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
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/ruby
DATA = File
.read('data.txt')
.split
.map(&:to_i)
def parse_node arr
num_children = arr[0]
num_metadata = arr[1]
children = []
metadata = []
cur_offset = 2
for n in (0...num_children)
c = parse_node arr[cur_offset..-1]
children << c
cur_offset += c[:size]
end
for n in (0...num_metadata)
metadata << arr[cur_offset]
cur_offset += 1
end
{
children: children,
metadata: metadata,
size: cur_offset,
}
end
def sum_metadata root
root[:metadata].sum + root[:children].map{ |c| sum_metadata c }.sum
end
PARSED = parse_node DATA
def sum_nodes root
return root[:metadata].sum if root[:children].size == 0
root[:metadata].map do |n|
n > root[:children].size ? 0 : sum_nodes(root[:children][n-1])
end.sum
end
PART1 = sum_metadata PARSED
PART2 = sum_nodes PARSED
puts 'Part 1: %s' % PART1
puts 'Part 2: %s' % PART2