-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19.pl
342 lines (314 loc) · 6.99 KB
/
19.pl
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/perl -w
use strict;
no warnings 'portable';
use Data::Dumper;
use feature 'say';
use Clipboard;
use List::Util qw/sum min max reduce any all none notall first product uniq/;
use Math::Cartesian::Product;
use Math::Complex;
use List::PriorityQueue;
use Memoize;
use POSIX qw/floor ceil/;
sub out {
my $out = shift;
if (ref($out)) {
print Dumper($out);
} else {
Clipboard->copy_to_all_selections($out);
print "$out\n";
}
}
# Utility function to make sure 2d array has equal rows
# input arr,fill. no output
sub equalize {
my $arr = shift;
my $fill = shift;
my $rows = @$arr;
my $cols = max(map {$_ ? scalar(@$_): 0} @$arr);
print "$rows $cols\n";
for my $row (@$arr) {
$row = [] unless $row;
while (@$row < $cols) {
push @$row, $fill;
}
}
}
# Find neighbors
# input neigh_arr, arr, row, col
# OR: neigh_arr, arr, "row,col"
# OR: neigh_arr, rows, cols, row, col
# OR: neigh_arr, rows, cols, "row,col"
# returns array of [row, col, value]
# OR: array of ["row,col", value]
# OR: array of "row,col"
sub neigh {
my $neigh = shift;
my ($rows,$cols);
my $arr = shift;
if (ref $arr) {
$rows = @$arr;
$cols = @{$arr->[0]};
} else {
$rows = $arr;
$cols = shift;
undef $arr;
}
my $row = shift;
my $col = shift;
my $comma;
if ($row =~ /(\d+)(\D+)(\d+)/) {
($row, $comma, $col) = ($1, $2, $3);
}
my @out;
for my $pair (@$neigh) {
my ($rd, $cd) = @$pair;
my ($nr, $nc) = ($row + $rd, $col + $cd);
next if $nr < 0;
next if $nc < 0;
next if $nr >= $rows;
next if $nc >= $cols;
if (defined($comma)) {
if ($arr) {
push @out, ["$nr$comma$nc", $arr->[$nr][$nc]];
} else {
push @out, "$nr$comma$nc";
}
} else {
push @out, [$nr, $nc, $arr ? ($arr->[$nr][$nc],) : ()];
}
}
return @out;
}
# Orthogonal
sub oneigh {
return neigh([[-1,0], [1, 0], [0, -1], [0, 1]], @_);
}
# All neighbors
sub aneigh {
return neigh([
[-1, -1], [-1, 0], [-1, 1],
[ 0, -1], [ 0, 1],
[ 1, -1], [ 1, 0], [ 1, 1]], @_);
}
# Numeric sort because sort defaults to lex
# returns new array
sub nsort {
my $in = \@_;
if (@$in == 1) {
$in = $in->[0];
}
return sort {$a <=> $b} @$in;
}
# Binary conversions
sub bin2dec {
my $in = shift;
return oct("0b$in");
}
sub dec2bin {
my $in = shift;
return sprintf ("%b", $in);
}
sub hex2bin {
my $in = shift;
return join('', map {sprintf("%04b", oct("0x$_"))} split('', $in));
}
sub bin2hex {
my $in = shift;
my $out;
my @in = split('', $in);
die "Bad bin value $in" if (@in%4);
while (@in) {
$out .= sprintf("%X", bin2dec(join('',splice(@in, 0, 4))));
}
return $out;
}
# A* / BFS implementation
# Args: start, end, neighbor function, heuristic function
# neighbor function: node -> [[new_node, cost], ...]
# cost assumed 1 if missing
# heuristic function: node -> lower bound on cost to end
sub astar {
my ($start, $end, $neigh, $h) = @_;
$h = sub {return 0;} unless $h;
my $OPEN = new List::PriorityQueue;
my %gscore = ($start, 0);
my %OHASH = ($start, 1);
my %path;
$OPEN->insert($start, $h->($start));
while (%OHASH) {
my $cur = $OPEN->pop();
delete $OHASH{$cur};
if ($cur eq $end) {
my $score = $gscore{$cur};
return $score unless wantarray;
my @path = ($cur);
while ($cur = $path{$cur}) {
unshift(@path, $cur)
}
return ($score, @path);
}
for my $n ($neigh->($cur)) {
my ($np,$v) = @$n;
if (!defined($v)) {
$v = 1;
}
my $new_g = $gscore{$cur} + $v;
if (!exists($gscore{$np}) || $new_g < $gscore{$np}) {
$path{$np} = $cur if wantarray;
$gscore{$np} = $new_g;
my $fscore = $new_g + $h->($np);
if (!$OHASH{$np}) {
$OPEN->insert($np, $fscore);
$OHASH{$np}++;
} else {
$OPEN->update($np, $fscore);
}
}
}
}
}
my @A;
my %H;
my $sum=0;
my @DONE;
BIG: while (<>) {
chomp;
m{scanner (\d+)};
my $sn=$1;
$_ = <>; chomp;
while ($_) {
push @{$A[$sn]}, [split(',')];
$_ = <>;
last BIG unless $_;
chomp;
}
}
my $b0 = shift @A;
my %h0;
for my $b (@$b0) {
$H{join(',', @$b)} = 1;
$h0{join(',', @$b)} = 1;
}
push @DONE, \%h0;
my @ROTLIST = (
[qw(-z -y -x)],
[qw(-z -x y)],
[qw(-z x -y)],
[qw(-z y x)],
[qw(-y -z x)],
[qw(-y -x -z)],
[qw(-y x z)],
[qw(-y z -x)],
[qw(-x -z -y)],
[qw(-x -y z)],
[qw(-x y -z)],
[qw(-x z y)],
[qw(x -z y)],
[qw(x -y -z)],
[qw(x y z)],
[qw(x z -y)],
[qw(y -z -x)],
[qw(y -x z)],
[qw(y x -z)],
[qw(y z x)],
[qw(z -y x)],
[qw(z -x -y)],
[qw(z x y)],
[qw(z y -x)]
);
sub rot {
my $b = shift;
my $r = shift;
my ($x,$y,$z) = @$b;
my $out = [map {my $q = $_; $q=~s{x}{$x}go;$q=~s{y}{$y}go;$q=~s{z}{$z}go;$q=~s{--}{}go; $q} @$r];
return $out;
}
sub allrots {
my $s = shift;
my @out;
for my $rot (@ROTLIST) {
push @out, [map {rot($_, $rot)} @$s];
}
return @out;
}
sub delta {
my ($p1, $p2) = @_;
return [$p1->[0] - $p2->[0], $p1->[1] - $p2->[1], $p1->[2] - $p2->[2]]
}
sub add {
my ($p1, $p2) = @_;
return [$p1->[0] + $p2->[0], $p1->[1] + $p2->[1], $p1->[2] + $p2->[2]]
}
memoize('allrots');
my @SCANNERS=([0,0,0]);
sub in_bounds {
my $p = shift;
SC: for my $s (@SCANNERS) {
for my $d (delta($p,$s)) {
next SC if ($d < -500 || $d > 500);
}
return 1;
}
return 0;
}
SIGNALS: while (@A) {
say "BIG LOOP";
#my $b1 = \%H;
for my $b1 (reverse(@DONE)) {
say "DONE loop";
for my $ib2 (0..$#A) {
say "ib2=$ib2";
for my $rb2 (allrots($A[$ib2])) {
for my $refstr (keys(%$b1)) {
my @ref = split(',',$refstr);
my $ref = \@ref;
# ref= reference beacon in canoncial coordinates
ALIGNMENT: for my $p (@$rb2) {
# p = seen beacon in scanner coords that we try to align with ref
my $delta = delta($ref, $p);
# delta = ref - p, which is -(location of ref in scanner coords)
my $matches = 0;
for my $p2 (@$rb2) {
# p2 new beacon in scanner coords
my $np = add($p2, $delta);
if ($b1->{join(',', @$np)}) {
$matches++;
}# elsif (in_bounds($np)) {
# next ALIGNMENT;
#}
}
if ($matches >= 12) {
say "FOUND IT! ib2 = $ib2";
push @SCANNERS, $delta;
my %h1;
for my $b (@$rb2) {
$b = add($b, $delta);
$H{join(',', @$b)} = 1;
$h1{join(',', @$b)} = 1;
}
push @DONE, \%h1;
splice(@A,$ib2,1);
next SIGNALS;
}
}
}
}
}
}
die;
}
say join("\n", keys %H);
say "------";
say join("\n", map {join(",", @$_)} @SCANNERS);
out (scalar(keys %H));
my $maxd=0;
for my $b1 (@SCANNERS) {
my ($x,$y,$z) = @$b1;
for my $b2 (@SCANNERS) {
my ($x2,$y2,$z2) = @$b2;
my $d = abs($x2-$x) + abs($y2-$y) + abs($z2-$z);
$maxd=$d if ($d > $maxd);
}
}
out($maxd);