#! C:/Perl/bin/perl ### Test for x objects in y containers where all objects are used ### Treats the containers y as n in a base n system and the objects x as the digits use strict; use warnings; use Math::Fleximal; #~ use Smart::Comments '###';#, '####' use YAML; $| = 1; my ( $Containers, $ObjectCount, $ContArrayRef, $AnswerHashRef ) = (); #### Get the container count while ( !$Containers ) { print "Please enter a positive number of Containers "; $Containers = <>; chomp $Containers; ### $Containers if ( $Containers ) { push @$ContArrayRef, "WBox$_" for (1..$Containers); ### $ContArrayRef $AnswerHashRef->{'boxes'} = $Containers; } } #### Get the object count my $CorrectObjectCount = 0; while ( $CorrectObjectCount == 0 or !$ObjectCount ) { print "Please enter the number of Objects "; $ObjectCount = <>; chomp $ObjectCount; if ($ObjectCount >= $Containers) { $CorrectObjectCount = 1; ### $ObjectCount $AnswerHashRef->{'objects'} = $ObjectCount; } else { print "You have not chosen enough objects to fill all containers\n"; } } #### Iterate through all possible combinations of all digits #### Start at 0 my $CountFleximal = Math::Fleximal->new( $ContArrayRef->[0], $ContArrayRef ); #### Fleck value one my $OneFleximal = $CountFleximal->one(); #### $OneFleximal #### build the Max Fleck for iteration my $MaxFlex; $MaxFlex .= $ContArrayRef->[$Containers - 1] for (1..$ObjectCount); ### $MaxFlex my $MaxFleximal = Math::Fleximal->new( $MaxFlex, $ContArrayRef ); #### Iterate through all possiblities while ( $CountFleximal->cmp($MaxFleximal) < 1) { #### sprintif for Fleximal to fill all digits my $SprintfFleximal; my @FleximalDigits = split "W", $CountFleximal->to_str; #### @FleximalDigits for (1..$ObjectCount) { my $NextFleck = pop @FleximalDigits; $NextFleck ||= $ContArrayRef->[0] ; $NextFleck =~ s/W(.+)/$1/; $SprintfFleximal = ( $SprintfFleximal ) ? $NextFleck . $SprintfFleximal : $NextFleck ; } ### $SprintfFleximal ### Check that all containers have objects my $test = 1; for my $i (1..$Containers) { if ( $SprintfFleximal !~ /Box$i/ ) { $test = 0 ; ### Box not represented: $i } } ### Load the answer to the output if valid if ( $test == 1 ) { my @ContainerOut = split "Box", $SprintfFleximal; shift @ContainerOut; ### @ContainerOut for my $object (0..$ObjectCount-1) { push @{$AnswerHashRef->{$SprintfFleximal}->{"Box". $ContainerOut[$object]}}, $object +1; } } $CountFleximal = $CountFleximal->add( $OneFleximal );#++ routine } ### $AnswerHashRef YAML::DumpFile ( 'Answer.txt', $AnswerHashRef ); 1;