-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08.rb
185 lines (152 loc) · 4.02 KB
/
day08.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
require_relative 'toolbox'
class Node
attr_accessor :header, :child_nodes, :metadata_entries
def value
v = 0
if child_nodes.empty?
v = sum_metadata
else
metadata_entries.each do |e|
next if e == 0
index = e - 1
child = child_nodes[index]
if child
v += child.value
end
end
end
v
end
def sum_metadata
child_sum = child_nodes.map{|c| c.sum_metadata }.flatten.reduce(:+)
child_sum ||= 0
metadata_entries.reduce(:+) + child_sum
end
end
class Tree
attr_reader :license
def load_file(f)
@license = raw_lines(f).first.split(' ').map(&:to_i)
end
def load_string(s)
@license = s.split(' ').map(&:to_i)
end
def root_node
return @root_node if @root_node
num_children = @license.shift
num_metadata = @license.shift
@root_node = Node.new
@root_node.header = [num_children, num_metadata]
if num_children == 0
@root_node.child_nodes = []
else
@root_node.child_nodes = _child_nodes(num_children, @license)
end
@root_node.metadata_entries = @license.slice!(0 - num_metadata, num_metadata)
@root_node
end
private
def _child_nodes(amount, license)
LOGGER.debug { "b - license: #{license.inspect}" }
nodes = []
amount.times do
num_children = license.shift
num_metadata = license.shift
n = Node.new
n.header = [num_children, num_metadata]
if num_children == 0
n.child_nodes = []
else
n.child_nodes = _child_nodes(num_children, license)
end
n.metadata_entries = license.slice!(0, num_metadata)
nodes << n
end
LOGGER.debug { "e - license: #{license.inspect} - nodes: #{nodes.inspect}" }
nodes
end
end
def run_tests
puts "Testing:"
t = Tree.new
t.load_string("0 1 99")
rn = t.root_node
test(2, rn.header.size)
test(0, rn.header[0])
test(1, rn.header[1])
test(0, rn.child_nodes.size)
test(1, rn.metadata_entries.size)
test([99], rn.metadata_entries)
t = Tree.new
t.load_string("0 3 10 11 12")
rn = t.root_node
test(2, rn.header.size)
test(0, rn.header[0])
test(3, rn.header[1])
test(0, rn.child_nodes.size)
test(3, rn.metadata_entries.size)
test([10,11,12], rn.metadata_entries)
t = Tree.new
t.load_string("2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2")
rn = t.root_node
test(2, rn.header.size)
test(2, rn.header[0])
test(3, rn.header[1])
test(2, rn.child_nodes.size)
test(3, rn.metadata_entries.size)
test([1,1,2], rn.metadata_entries)
test(138, rn.sum_metadata)
t = Tree.new
t.load_string("1 1 0 1 99 2")
rn = t.root_node
test(2, rn.header.size)
test(1, rn.header[0])
test(1, rn.header[1])
test(1, rn.child_nodes.size)
test(1, rn.metadata_entries.size)
test([2], rn.metadata_entries)
t = Tree.new
t.load_string("2 1 0 1 99 0 1 99 16")
rn = t.root_node
test(2, rn.header.size)
test(2, rn.header[0])
test(1, rn.header[1])
test(2, rn.child_nodes.size)
test(1, rn.metadata_entries.size)
test([16], rn.metadata_entries)
rn.child_nodes.each do |n|
test(2, n.header.size)
test(0, n.header[0])
test(1, n.header[1])
test(0, n.child_nodes.size)
test(1, n.metadata_entries.size)
test([99], n.metadata_entries)
end
t = Tree.new
t.load_string("2 2 2 1 0 1 99 0 1 99 16 2 1 0 1 99 0 1 99 16 32 32")
rn = t.root_node
test(2, rn.header.size)
test(2, rn.header[0])
test(2, rn.header[1])
test(2, rn.child_nodes.size)
test(2, rn.metadata_entries.size)
test([32, 32], rn.metadata_entries)
rn.child_nodes.each do |n|
test(2, n.header.size)
test(2, n.header[0])
test(1, n.header[1])
test(2, n.child_nodes.size)
test(1, n.metadata_entries.size)
test([16], n.metadata_entries)
end
puts
end
run_tests
#f = 'data/day08_test_license.txt'
f = 'data/day08_production_license.txt'
t = Tree.new
t.load_file(f)
puts "Part 1: What is the sum of all metadata entries?"
puts "Answer: #{t.root_node.sum_metadata}"
puts "Part 2: What is the value of the root node?"
puts "Answer: #{t.root_node.value}"