11 if (/--- (scanner \d+) ---/) {
13 $first_scanner //= $1;
17 $scans{$scanner}->{$_} = 1;
35 $r[3] = $_ & 0x01 ? -1 : 1;
36 $r[4] = $_ & 0x02 ? -1 : 1;
37 $r[5] = $_ & 0x04 ? -1 : 1;
43 say "rotations: ", scalar @rotations;
46 my ($coords, $trans) = @_;
48 for (0 .. $#$coords) {
49 $res[$_] = $coords->[$trans->[$_]];
50 $res[$_] *= -1 if $trans->[$_+@$coords] < 0;
57 my $coords1 = $scans{$s1};
58 my $coords2 = $scans{$s2};
60 say "comparing $s1 and $s2";
61 for my $from_t (keys %$coords1) {
62 my @from = split /,/, $from_t;
63 for my $rot (@rotations) {
64 my @from_rotated = transform(\@from, $rot);
65 # say "rotated ", join(',', @from), ' to ', join(',', @from_rotated);
66 for my $to_t (keys %$coords2) {
67 my @off = split /,/, $to_t;
68 $off[$_] -= $from_rotated[$_] for 0..$#off;
69 # say "trying offset ", join(',', @off);
71 for my $c1_t (keys %$coords1) {
72 my @c1 = split /,/, $c1_t;
73 @c1 = transform(\@c1, $rot);
74 $c1[$_] += $off[$_] for 0..$#off;
75 # say "searching for ", join(',',@c1);
76 $count++ if $coords2->{join(',', @c1)};
79 say "found $count matches ", join(',', @$rot);
87 my %aligned_scanners = ($first_scanner => [ [ 0, 0, 0], [ 0, 1, 2, 1, 1, 1 ] ]);
88 my %beacons = ( %{ $scans{$first_scanner} } );
90 while (scalar keys %aligned_scanners < scalar keys %scans) {
91 say scalar keys %beacons, ' beacons found:';
92 for my $s1 (grep { $aligned_scanners{$_} } keys %scans) {
93 for my $s2 (grep { !$aligned_scanners{$_} } keys %scans) {
94 next if $tested{$s1,$s2} || $tested{$s2,$s1};
95 my ($off, $rot) = is_aligned($s2, $s1);
96 $tested{$s1,$s2} = $tested{$s2,$s1} = 1;
97 next if !defined $rot;
99 for my $c2_t (keys %{ $scans{$s2}}) {
100 my @c2 = split /,/, $c2_t;
101 @c2 = transform(\@c2, $rot);
102 $c2[$_] += $off->[$_] for 0 .. $#c2;
103 my $key = join(',', @c2);
107 $scans{$s2} = \%nscans;
108 $aligned_scanners{$s2} = 1;
109 say "$s1 and $s2 aligned: ", join(',', @$off, @$rot), " beacons: ", scalar keys %beacons;
115 for my $b (sort keys %beacons) {
118 say "total beacons: ", scalar keys %beacons;