-
Notifications
You must be signed in to change notification settings - Fork 0
/
19a_solution.rb
233 lines (199 loc) · 5 KB
/
19a_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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
require 'set'
filename = "19input.txt"
num_beacons = 12
dirs = [1, -1]
def unit(i, sign)
[0, 1, 2].map do |j|
j == i ? sign : 0
end
end
def cross_prod(a, b)
[
a[1]*b[2]-a[2]*b[1],
a[2]*b[0]-a[0]*b[2],
a[0]*b[1]-a[1]*b[0]
]
end
def right_handed_dir(perm, sign1, sign2)
cross_prod(
unit(perm[0], sign1),
unit(perm[1], sign2)
)[perm[2]]
end
$orientations = dirs.flat_map do |e1|
dirs.flat_map do |e2|
[0, 1, 2].permutation.map do |perm|
{ perm: perm, dir: [e1, e2, right_handed_dir(perm, e1, e2)] }
end
end
end
default_orientation = { perm: [0, 1, 2], dir: [1, 1, 1] }
def opposite_orientation(orientation)
{
perm: orientation[:perm],
dir: orientation[:dir].map { |e| -e }
}
end
def invert(orientation)
perm = orientation[:perm]
dir = orientation[:dir]
inverse_perm = [perm.index(0), perm.index(1), perm.index(2)]
{
perm: inverse_perm,
dir: [dir[inverse_perm[0]], dir[inverse_perm[1]], dir[inverse_perm[2]]]
}
end
def compose(o1, o2)
{
perm: [
o1[:perm][o2[:perm][0]],
o1[:perm][o2[:perm][1]],
o1[:perm][o2[:perm][2]]
],
dir: [
o2[:dir][0] * o1[:dir][o2[:perm][0]],
o2[:dir][1] * o1[:dir][o2[:perm][1]],
o2[:dir][2] * o1[:dir][o2[:perm][2]]
]
}
end
def apply(orientation, point)
perm = orientation[:perm]
dir = orientation[:dir]
[point[perm[0]] * dir[0], point[perm[1]] * dir[1], point[perm[2]] * dir[2]]
end
def try_overlap(scanners, paths, a_idx, b_idx, size)
scanner_a = scanners[a_idx]
scanner_b = scanners[b_idx]
scanner_b_pos, scanner_b_orientation = try_match(scanner_a, scanner_b, size)
if !scanner_b_pos.nil?
paths[b_idx] << {
from: a_idx,
pos: scanner_b_pos,
orientation: scanner_b_orientation
}
inverted_pos = scanner_b_pos.map { |e| -e }
paths[a_idx] << {
from: b_idx,
pos: apply(invert(scanner_b_orientation), inverted_pos),
orientation: invert(scanner_b_orientation)
}
end
end
def try_match(a_beacons, b_beacons, size)
$orientations.each do |orientation|
a_beacons.each do |first_a|
b_beacons.each do |first_b|
opposite = opposite_orientation(orientation)
b_pos_from_a = move(first_a, first_b, opposite)
b_beacons_from_a = b_beacons
.map { |b| move(b_pos_from_a, b, orientation) }
.filter { |b| b.all? { |pos| pos.abs <= 1000 } }
inter = Set.new(b_beacons_from_a).intersection(Set.new(a_beacons))
if inter.count >= size
return [b_pos_from_a, orientation]
end
end
end
end
[nil, nil]
end
def move(a, b, orientation)
perm = orientation[:perm]
shuffled = [a[perm[0]], a[perm[1]], a[perm[2]]]
shifted = shuffled.each_with_index.map do |e, i|
e + orientation[:dir][i] * b[i]
end
[shifted[perm.index(0)], shifted[perm.index(1)], shifted[perm.index(2)]]
end
def overlap_all(scanners, paths, size)
indices = (0...scanners.count)
index_pairs = indices.flat_map do |i|
indices.map do |j|
if i>=j
nil
else
[i, j]
end
end.compact
end
index_pairs.each do |i1, i2|
puts "try overlapping #{i1} and #{i2}"
try_overlap(scanners, paths, i1, i2, size)
end
end
def reduce(scanner_data)
loop do
result = reduce_once(scanner_data)
break if result.nil?
result[0] << { from: 0, pos: result[1], orientation: result[2] }
end
end
def reduce_once(paths)
without_zero(paths).each do |target_paths|
target_paths.each do |end_path|
from_data = paths[end_path[:from]]
zero_to_from = zero_path(from_data)
if !zero_to_from.nil?
new_path = move(
zero_to_from[:pos],
end_path[:pos],
zero_to_from[:orientation]
)
new_orientation = compose(zero_to_from[:orientation], end_path[:orientation])
return [target_paths, new_path, new_orientation]
end
end
end
nil
end
def without_zero(all_paths)
all_paths.filter do |paths|
zero_path(paths).nil?
end
end
def zero_path(paths)
paths.find do |path|
path[:from] == 0
end
end
def parse_input(filename)
parts = File.read(filename).split("\n\n")
parts.map do |part|
part.split("\n")[1..].map do |line|
line.split(",").map(&:to_i)
end
end
end
def scanner_positions(all_paths)
all_paths.map do |paths|
p = paths.find do |path|
path[:from] == 0
end
!p.nil? ? p[:pos] : nil
end
end
def find_beacons(beacons, scanners, paths, scanner_pos)
scanners.each_with_index do |scanner, i|
scanner.each do |beacon|
orientation = zero_path(paths[i])[:orientation]
beacon_abs = move(scanner_pos[i], beacon, orientation)
beacons.add beacon_abs
end
end
end
scanners = parse_input(filename)
paths = Array.new(scanners.length) { [] }
paths[0] = [
{
from: 0,
pos: [0,0,0],
orientation: default_orientation
}
]
overlap_all(scanners, paths, num_beacons)
reduce(paths)
scanner_pos = scanner_positions(paths)
beacons = Set.new
find_beacons(beacons, scanners, paths, scanner_pos)
puts beacons.count