#!/usr/bin/perl -w

use v5.36;
use strict;
use List::Util qw(sum);
use experimental 'multidimensional';

my @points = map { chomp; [ split /,/ ] } <>;

sub dist($p1, $p2) { my $sum; $sum+=abs($p1->[$_]-$p2->[$_]) for 0 .. 3; $sum };

my $n_const;
my %const_of;
for my $x (0 .. $#points) {
	for my $y ($x .. $#points) {
		next if dist($points[$x], $points[$y]) > 3;
		say "dist($x,$y) = ", dist($points[$x], $points[$y]);
		if (!defined $const_of{$x} && !defined $const_of{$y}) {
			$const_of{$x} = $const_of{$y} = ++$n_const;
		} elsif (!defined $const_of{$x}) {
			$const_of{$x} = $const_of{$y};
		} elsif (!defined $const_of{$y}) {
			$const_of{$y} = $const_of{$x};
		} elsif ($const_of{$x} != $const_of{$y}) {
			# both different, join them
			say "adding $const_of{$y} to $const_of{$x}";
			my $to_del = $const_of{$y};
			for my $p (0 .. $#points) {
				$const_of{$p} = $const_of{$x}
					if defined $const_of{$p} 
						&& $const_of{$p} == $to_del;
			}
		}
		say "     $x in $const_of{$x}, $y in $const_of{$y}";
	}
}

say "$_ in $const_of{$_}" for sort keys %const_of;
my %const_used = map { $_ => 1 } values %const_of;
say join(',', keys %const_used);
say scalar keys %const_used;

