aboutsummaryrefslogtreecommitdiff
path: root/challenge-036/duane-powell/perl5/ch-2.pl
blob: 6312aab6ed1671844df7a9328a5f6ec2f5490932 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/perl
use warnings;
use strict;
use feature qw( say );
use Math::Combinatorics;

# Write a program to solve Knapsack Problem.

# Solution: Create a self packing Knapsack object that fills itself with the best combination of boxes.  
# This code solves for both cases of box availability: use each box once or reuse box types up to count and weight limits.

my $capacity  = shift;
my $box_reuse = shift;
do {
        print <<EOU;
Usage:
$0 capacity reuse-box
	$0 15 1
	$0 15 0
EOU
        exit;
} unless $capacity;


my $BOX = { # Global data structure
	R => { weight => 1, amount => 1 },
	B => { weight => 1, amount => 2 },
	G => { weight => 2, amount => 2 },
	Y => { weight =>12, amount => 4 },
	P => { weight => 4, amount =>10 },
};
my @box = (sort keys %$BOX);

say "Problem:";
say "Box\tWeight\tAmount";
foreach my $box (@box) {
	say "$box\t$BOX->{$box}{weight}\t$BOX->{$box}{amount}";
}

# Create new Knapsack, pack it and say best fit	for all $c combinations: 2, 3, 4 and 5 boxes
my $knapsack;
my $c;
my $out = $box_reuse ? "box types" : "boxes limit";

say "\nSolution: (2 $out)";
$knapsack = Knapsack->new($capacity,2,$box_reuse);
$c = Math::Combinatorics->new(count => 2, data => [@box]);
while (my @combo = $c->next_combination) {
	$knapsack->pack(@combo);
} 
$knapsack->box_list(); 


say "\nSolution: (3 $out)";
$knapsack = Knapsack->new($capacity,3,$box_reuse);
$c = Math::Combinatorics->new(count => 3, data => [@box]);
while (my @combo = $c->next_combination) {
	$knapsack->pack(@combo);
}   
$knapsack->box_list();


say "\nSolution: (4 $out)";
$knapsack = Knapsack->new($capacity,4,$box_reuse);
$c = Math::Combinatorics->new(count => 4, data => [@box]);
while (my @combo = $c->next_combination) {
	$knapsack->pack(@combo);
}   
$knapsack->box_list();


say "\nSolution: (5 $out)";
$knapsack = Knapsack->new($capacity,5,$box_reuse);  
$knapsack->pack(@box);
$knapsack->box_list();
exit;

package Knapsack;
sub new {
        my $class = shift;
        my $self = {
		capacity    => shift,
		box_max     => shift,
		box_reuse   => shift,
		box_list    => '', # current state of the object
		weight      => 0, 
		amount      => 0,
		best_list   => '', # best state reached
		best_weight => 0, 
		best_amount => 0,
	};
	bless $self, $class;
	return $self;
}

sub pack {
	my ($self,@box) = @_;
	if ($self->{box_reuse}) {
		$self->pack_reuse(@box);
	}
	else {
		$self->pack_solo(@box);
	}
}
sub pack_reuse {
	my ($self,@box) = @_;
        foreach my $box (@box) {
		if ($self->box_will_fit($box)) {
			$self->box_add($box);
			$self->pack_reuse(@box);
			$self->box_sub($box);
		}
	}
}
sub pack_solo {
	my ($self,@box) = @_;
        foreach my $box (@box) {
		next if ($self->{box_list} =~ m/$box/); # can not use same box twice
		if ($self->{box_max} > length($self->{box_list})) { # can not exceed number of boxes allowed
			if ($self->box_will_fit($box)) {
				$self->box_add($box);
				$self->pack_solo(@box);
				$self->box_sub($box);
			}
		}
	}
}
sub box_add {
	my ($self,$box) = @_;
	$self->{weight}   += $BOX->{$box}{weight};
	$self->{amount}   += $BOX->{$box}{amount};
	$self->{box_list} .= $box;
	if ($self->{amount} > $self->{best_amount} ) {
		 $self->{best_amount} = $self->{amount};
		 $self->{best_weight} = $self->{weight};
		 $self->{best_list}   = $self->{box_list};
	}
}
sub box_sub {
	my ($self,$box) = @_;
	$self->{weight} -= $BOX->{$box}{weight};
	$self->{amount} -= $BOX->{$box}{amount};
	chop $self->{box_list};
}
sub box_list {
	my $self = shift;
	say "Box list:     $self->{best_list}";
	say "Total weight: $self->{best_weight}";
	say "Total amount: $self->{best_amount}";
}
sub box_will_fit {
	my ($self,$box) = @_;
	return ($self->{capacity} >= $self->{weight} + $BOX->{$box}{weight});
}
1;

__END__

./ch-2.pl
Usage:
./ch-2.pl capacity reuse-box
        ./ch-2.pl 15 1
        ./ch-2.pl 15 0


./ch-2.pl 15 1
Problem:
Box     Weight  Amount
B       1       2
G       2       2
P       4       10
R       1       1
Y       12      4

Solution: (2 box types)
Box list:     BBBPPP
Total weight: 15
Total amount: 36

Solution: (3 box types)
Box list:     BBBPPP
Total weight: 15
Total amount: 36

Solution: (4 box types)
Box list:     PPPBBB
Total weight: 15
Total amount: 36

Solution: (5 box types)
Box list:     BBBPPP
Total weight: 15
Total amount: 36


./ch-2.pl 15 0
Problem:
Box     Weight  Amount
B       1       2
G       2       2
P       4       10
R       1       1
Y       12      4

Solution: (2 boxes limit)
Box list:     BP
Total weight: 5
Total amount: 12

Solution: (3 boxes limit)
Box list:     BGP
Total weight: 7
Total amount: 14

Solution: (4 boxes limit)
Box list:     GRPB
Total weight: 8
Total amount: 15

Solution: (5 boxes limit)
Box list:     BGPR
Total weight: 8
Total amount: 15