-
Notifications
You must be signed in to change notification settings - Fork 0
/
18a_solution.rb
172 lines (139 loc) · 3.09 KB
/
18a_solution.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
def parse_input(input)
num_match = /^(\d+)/.match(input)
if !num_match.nil?
num = num_match[1]
[num.to_i, num.length]
else
first, first_length = parse_input(input[1..])
second, second_length = parse_input(input[2+first_length..])
[[first, second], 3+first_length+second_length]
end
end
def get(a, idx)
idx.inject(a) do |acc, i|
acc[i]
end
end
def set(a, idx, value)
outer = get(a, idx[0..-2])
outer[idx.last] = value
end
def lt(idx1, idx2)
idx1.zip(idx2).each do |i1, i2|
return false if i1.nil? || i2.nil? # not comparable
return true if i1 < i2
return false if i1 > i2
end
false
end
def gt(idx1, idx2)
idx1.zip(idx2).each do |i1, i2|
return false if i1.nil? || i2.nil? # not comparable
return true if i1 > i2
return false if i1 < i2
end
false
end
def first(a, idx, &pred)
first_idx = idx + [0]
second_idx = idx + [1]
result_idx = nil
if pred.call(first_idx)
result_idx = first_idx
end
if result_idx.nil? && get(a, first_idx).is_a?(Array)
result_idx = first(a, first_idx, &pred)
end
if result_idx.nil? && pred.call(second_idx)
result_idx = second_idx
end
if result_idx.nil? && get(a, second_idx).is_a?(Array)
result_idx = first(a, second_idx, &pred)
end
result_idx
end
def last(a, idx, &pred)
first_idx = idx + [0]
second_idx = idx + [1]
result_idx = nil
if get(a, second_idx).is_a?(Array)
result_idx = last(a, second_idx, &pred)
end
if result_idx.nil? && pred.call(second_idx)
result_idx = second_idx
end
if result_idx.nil? && get(a, first_idx).is_a?(Array)
result_idx = last(a, first_idx, &pred)
end
if result_idx.nil? && pred.call(first_idx)
result_idx = first_idx
end
result_idx
end
def explode(a)
pair_idx = first(a, []) do |idx|
idx.count >= 4 && get(a, idx).is_a?(Array)
end
return false if pair_idx.nil?
pair = get(a, pair_idx)
left_idx = last(a, []) do |idx|
lt(idx, pair_idx) && !get(a, idx).is_a?(Array)
end
if !left_idx.nil?
old_val = get(a, left_idx)
set(a, left_idx, old_val + pair[0])
end
right_idx = first(a, []) do |idx|
gt(idx, pair_idx) && !get(a, idx).is_a?(Array)
end
if !right_idx.nil?
old_val = get(a, right_idx)
set(a, right_idx, old_val + pair[1])
end
set(a, pair_idx, 0)
true
end
def split(a)
split_idx = first(a, []) do |idx|
val = get(a, idx)
!val.is_a?(Array) && val >= 10
end
return false if split_idx.nil?
split_val = get(a, split_idx)
lower = (split_val/2.0).floor
upper = (split_val/2.0).ceil
set(a, split_idx, [lower, upper])
true
end
def reduce(a)
done = false
while !done
exploded = explode(a)
if !exploded
splitted = split(a)
if !splitted
done = true
end
end
end
end
def add(a, b)
sum = [a, b]
reduce(sum)
sum
end
def add_all(nums)
nums[1..].inject(nums[0]) do |a, e|
add(a, e)
end
end
def magnitude(a)
if a.is_a?(Array)
3*magnitude(a[0])+2*magnitude(a[1])
else
a
end
end
inputs = File.read("18input.txt").split("\n").map { |l| parse_input(l)[0] }
sum = add_all(inputs)
puts magnitude(sum)