-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19-1.rb
executable file
·225 lines (183 loc) · 4.29 KB
/
19-1.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
#!/usr/bin/env ruby
require 'matrix'
class Scanner
attr_accessor :beacons
attr_accessor :distances
attr_accessor :id
def initialize(id)
@id = id
@beacons = []
@distances = []
end
def add_beacon(x, y, z)
@beacons.append Matrix[[x], [y], [z]]
end
def calculate_distances!
@beacons.permutation(2) do |b1, b2|
@distances.append (b1[0, 0] - b2[0, 0]).abs**2 +
(b1[1, 0] - b2[1, 0]).abs**2 +
(b1[2, 0] - b2[2, 0]).abs**2
end
@distances.sort!
end
def match_distances(other)
matching = 0
@distances.each { |d| matching += 1 if other.distances.bsearch { |e| d <=> e } }
matching
end
def match_distances?(other)
match_distances(other) >= 66 # 66 == 12 choose 2
end
def all_rotations
# https://stackoverflow.com/a/51836928
as = [[[1, 0, 0],
[0, 1, 0],
[0, 0, 1]],
[[0, 1, 0],
[0, 0, 1],
[1, 0, 0]],
[[0, 0, 1],
[1, 0, 0],
[0, 1, 0]]].map { |m| Matrix[*m] }
bs = [[[1, 0, 0],
[0, 1, 0],
[0, 0, 1]],
[[-1, 0, 0],
[ 0,-1, 0],
[ 0, 0, 1]],
[[-1, 0, 0],
[ 0, 1, 0],
[ 0, 0,-1]],
[[ 1, 0, 0],
[ 0,-1, 0],
[ 0, 0,-1]]].map { |m| Matrix[*m] }
cs = [[[1, 0, 0],
[0, 1, 0],
[0, 0, 1]],
[[ 0, 0,-1],
[ 0,-1, 0],
[-1, 0, 0]]].map { |m| Matrix[*m] }
rotations = []
as.each do |a|
bs.each do |b|
cs.each do |c|
rotations.append a * b * c
end
end
end
rotations
end
def match_coords(other)
all_rotations.each do |rotation|
rotated = other.beacons.map { |coords| rotation * coords }
rotated.each do |r|
@beacons.each do |b|
translation = r - b
translated = 0
to_translate = @beacons.dup
until to_translate.empty?
coords = to_translate.shift
if rotated.any? (coords + translation)
translated += 1
end
end
return [true, rotation, translation] if translated >= 12
end
end
end
[false, nil, nil]
end
def transform!(rotation, translation)
@beacons = transformed_coords(rotation, translation)
end
def transformed_coords(rotation, translation)
@beacons.map do |b|
rotation * b - translation
end
end
def to_s
s = "<#{self.class}: #{@id}\n"
@beacons.each do |b|
s += "(#{b[0, 0]}, #{b[1, 0]}, #{b[2, 0]})\n"
end
s += "\n#{@distances}\n>"
s
end
def inspect
to_s
end
end
class Map
attr_accessor :map
def initialize
@map = {}
end
def mark(beacon)
self[beacon[0, 0],
beacon[1, 0],
beacon[2, 0]] = 1
end
def [](x, y, z)
@map[[x, y, z]]
end
def []=(x, y, z, val)
@map[[x, y, z]] = val
end
def score
@map.values.size
end
def to_s
s = "<#{self.class}:\n"
@map.each_key do |m|
s += "(#{m[0]}, #{m[1]}, #{m[2]})\n"
end
s += "\nScore: #{score}\n>"
s
end
def inspect
to_s
end
end
header_format = /^--- scanner (?<id>[[:digit:]]+) ---$/
beacon_format = /^(?<x>[[:digit:]\-]+),(?<y>[[:digit:]\-]+),(?<z>[[:digit:]\-]+)$/
input = File.read('19.input').lines.map(&:strip)
scanners = []
until input.empty?
h = header_format.match input.shift
id = h['id'].to_i
s = Scanner.new(id)
loop do
l = input.shift
break if not l or l.empty?
b = beacon_format.match l
s.add_beacon(b['x'].to_i, b['y'].to_i, b['z'].to_i)
end
s.calculate_distances!
scanners.append s
end
map = Map.new
scanners[0].beacons.each { |b| map.mark b }
checked = []
locked = [scanners[0]]
to_check = scanners[1..]
until to_check.empty?
found = []
locked.each do |l|
to_check.each do |t|
next if l.id == t.id
next if checked.any? [l.id, t.id] or checked.any? [t.id, l.id]
checked.append [l.id, t.id]
if l.match_distances? t
res, rotation, translation = l.match_coords t
if res
found.append t
t.transform!(rotation, translation)
t.beacons.each { |c| map.mark c }
end
end
end
end
locked += found
to_check -= found
end
print map.score, "\n"