5 use List::Util qw(sum);
6 use experimental 'multidimensional';
8 my @points = map { chomp; [ split /,/ ] } <>;
10 sub dist($p1, $p2) { my $sum; $sum+=abs($p1->[$_]-$p2->[$_]) for 0 .. 3; $sum };
14 for my $x (0 .. $#points) {
15 for my $y ($x .. $#points) {
16 next if dist($points[$x], $points[$y]) > 3;
17 say "dist($x,$y) = ", dist($points[$x], $points[$y]);
18 if (!defined $const_of{$x} && !defined $const_of{$y}) {
19 $const_of{$x} = $const_of{$y} = ++$n_const;
20 } elsif (!defined $const_of{$x}) {
21 $const_of{$x} = $const_of{$y};
22 } elsif (!defined $const_of{$y}) {
23 $const_of{$y} = $const_of{$x};
24 } elsif ($const_of{$x} != $const_of{$y}) {
25 # both different, join them
26 say "adding $const_of{$y} to $const_of{$x}";
27 my $to_del = $const_of{$y};
28 for my $p (0 .. $#points) {
29 $const_of{$p} = $const_of{$x}
30 if defined $const_of{$p}
31 && $const_of{$p} == $to_del;
34 say " $x in $const_of{$x}, $y in $const_of{$y}";
38 say "$_ in $const_of{$_}" for sort keys %const_of;
39 my %const_used = map { $_ => 1 } values %const_of;
40 say join(',', keys %const_used);
41 say scalar keys %const_used;