#!/usr/bin/perl -w

use v5.16;

$; = ',';

sub transform {
	my ($coords, $trans) = @_;
	my @res;
	for (0 .. $#$coords) {
		$res[$_] = $coords->[$trans->[$_]];
		$res[$_] *= -1 if $trans->[$_+@$coords] < 0;
	}
	return @res;
}

my %aligned_scanners = ('scanner 0' => [ 0, 0, 0 ]);

# parsing the debugging output of the first part ...
while (<>) {
	my ($s1, $s2, $rest) = /(.*) and (.*) aligned: (.*) beacons:/;
	next if !$rest;

	my @r = split /,/, $rest;
	my @off = @r[0..2];
	my @rot = @r[3..8];

	# @off = transform(\@off, \@rot);
	say "$s1 to $s2";
	# $off[$_] += $aligned_scanners{$s1}->[$_] for 0..2;
	say "$s2 at ", join(',', @off);
	$aligned_scanners{$s2} = [ @off ];
}

my $max_dist = 0;
for my $s1 (keys %aligned_scanners) {
for my $s2 (keys %aligned_scanners) {
	my $dist;
	$dist += abs($aligned_scanners{$s1}->[$_] - $aligned_scanners{$s2}->[$_]) for 0 .. 2;
	$max_dist = $dist if $dist > $max_dist;
	say "$s1 to $s2 = $dist";
} }

say $max_dist;

