#!/usr/bin/perl -w
use strict;

my @a = ("red", "red", "green", "blue", "blue", "red", "orange");

print "Distance: 1; pairs: ", distant_pairs(1, @a), "\n";
print "Distance: 2; pairs: ", distant_pairs(2, @a), "\n";
print "Distance: 3; pairs: ", distant_pairs(3, @a), "\n";
print "Distance: 4; pairs: ", distant_pairs(4, @a), "\n";
print "Distance: 5; pairs: ", distant_pairs(5, @a), "\n";
print "Distance: 6; pairs: ", distant_pairs(6, @a), "\n";
print "Distance: 7; pairs: ", distant_pairs(7, @a), "\n";
print "Distance: 8; pairs: ", distant_pairs(8, @a), "\n";

sub distant_pairs {
   my ($distance, @subject) = @_;
   my $count = 0;
   for (my $j = 1; $j < @subject; $j = $j + 1) {
      if ($subject[$j] eq $subject[$j-1]) {
         $count = $count + 1;
      }
   }
   return $count;
}
