package main;

use strict;
use warnings;


my @gPeople = sort ('Sue', 'Jim', 'Pat', 'Don', 'Bob', 'Joe', 'Liz', 'Ned');
use constant kMaxGroupSize => 6;


my $solution_count = 0;
my %seen_solution = ();

sub permute_people
{
	my ($group_size, $items_list_ref, $perms_list_ref) = @_;
	
	my @items = @{ $items_list_ref };
	my @perms = @{ $perms_list_ref };
	
	if (scalar(@perms) == $group_size) 
	{
		my @perms = sort @perms;
			
		return if (grep($_ eq 'Bob', @perms) && grep($_ eq 'Joe', @perms));
		return if (grep($_ eq 'Liz', @perms) && grep($_ eq 'Ned', @perms));
		
		if (!$seen_solution{"@perms"}++)
		{
			$solution_count++;
			print "  @perms\n";
		}			
	}
	else 
	{
		my (@newitems, @newperms, $selected);
		
		foreach my $i (0 .. $#items)
		{
			@newitems = @items;
			@newperms = @perms;
			
			$selected = splice(@newitems, $i, 1);
			push(@newperms, $selected);
			permute_people($group_size, [@newitems], [@newperms]);
		}
	}
}


sub iterate_group_size
{
	my ($max_group_size, $people) = @_;

	foreach my $i (1 .. $max_group_size)
	{
		print "\nGroup size: $i\n";
		permute_people($i, \@gPeople, []);
	}
}


iterate_group_size(kMaxGroupSize, scalar(@gPeople));

print "\nSolutions: $solution_count\n";

