Skip to content

Commit 6e3c6b3

Browse files
glennjm-dango
andauthored
Add two-bucket exercise (#692)
* Add two-bucket exercise --------- Co-authored-by: Daniel Mita <[email protected]>
1 parent a9d816f commit 6e3c6b3

File tree

9 files changed

+349
-0
lines changed

9 files changed

+349
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,14 @@
742742
"prerequisites": [],
743743
"difficulty": 1
744744
},
745+
{
746+
"slug": "two-bucket",
747+
"name": "Two Bucket",
748+
"uuid": "1ac50ef0-d6c8-4217-867f-3a72a353ac93",
749+
"practices": [],
750+
"prerequisites": [],
751+
"difficulty": 4
752+
},
745753
{
746754
"slug": "two-fer",
747755
"name": "Two Fer",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Instructions
2+
3+
Given two buckets of different size and which bucket to fill first, determine how many actions are required to measure an exact number of liters by strategically transferring fluid between the buckets.
4+
5+
There are some rules that your solution must follow:
6+
7+
- You can only do one action at a time.
8+
- There are only 3 possible actions:
9+
1. Pouring one bucket into the other bucket until either:
10+
a) the first bucket is empty
11+
b) the second bucket is full
12+
2. Emptying a bucket and doing nothing to the other.
13+
3. Filling a bucket and doing nothing to the other.
14+
- After an action, you may not arrive at a state where the initial starting bucket is empty and the other bucket is full.
15+
16+
Your program will take as input:
17+
18+
- the size of bucket one
19+
- the size of bucket two
20+
- the desired number of liters to reach
21+
- which bucket to fill first, either bucket one or bucket two
22+
23+
Your program should determine:
24+
25+
- the total number of actions it should take to reach the desired number of liters, including the first fill of the starting bucket
26+
- which bucket should end up with the desired number of liters - either bucket one or bucket two
27+
- how many liters are left in the other bucket
28+
29+
Note: any time a change is made to either or both buckets counts as one (1) action.
30+
31+
Example:
32+
Bucket one can hold up to 7 liters, and bucket two can hold up to 11 liters.
33+
Let's say at a given step, bucket one is holding 7 liters and bucket two is holding 8 liters (7,8).
34+
If you empty bucket one and make no change to bucket two, leaving you with 0 liters and 8 liters respectively (0,8), that counts as one action.
35+
Instead, if you had poured from bucket one into bucket two until bucket two was full, resulting in 4 liters in bucket one and 11 liters in bucket two (4,11), that would also only count as one action.
36+
37+
Another Example:
38+
Bucket one can hold 3 liters, and bucket two can hold up to 5 liters.
39+
You are told you must start with bucket one.
40+
So your first action is to fill bucket one.
41+
You choose to empty bucket one for your second action.
42+
For your third action, you may not fill bucket two, because this violates the third rule -- you may not end up in a state after any action where the starting bucket is empty and the other bucket is full.
43+
44+
Written with <3 at [Fullstack Academy][fullstack] by Lindsay Levine.
45+
46+
[fullstack]: https://www.fullstackacademy.com/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"authors": [
3+
"glennj"
4+
],
5+
"contributors": [
6+
"m-dango"
7+
],
8+
"files": {
9+
"solution": [
10+
"lib/TwoBucket.pm"
11+
],
12+
"test": [
13+
"t/two-bucket.t"
14+
],
15+
"example": [
16+
".meta/solutions/lib/TwoBucket.pm"
17+
]
18+
},
19+
"blurb": "Given two buckets of different size, demonstrate how to measure an exact number of liters.",
20+
"source": "Water Pouring Problem",
21+
"source_url": "https://demonstrations.wolfram.com/WaterPouringProblem/"
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package TwoBucket;
2+
3+
use strict;
4+
use warnings;
5+
use experimental qw<signatures postderef postderef_qq>;
6+
7+
use Exporter qw<import>;
8+
our @EXPORT_OK = qw<measure>;
9+
10+
use List::Util qw(max min);
11+
12+
sub gcd ( $a, $b ) {
13+
while ( $b > 0 ) {
14+
( $a, $b ) = ( $b, $a % $b );
15+
}
16+
return $a;
17+
}
18+
19+
sub validate ( $b1, $b2, $goal ) {
20+
die 'impossible: goal too big' if $goal > max( $b1, $b2 );
21+
my $g = gcd $b1, $b2;
22+
die 'impossible: goal unsatisfiable' if $g > 1 && $goal % $g != 0;
23+
}
24+
25+
sub fill ($bucket) { $bucket->{amount} = $bucket->{size}; }
26+
sub empty ($bucket) { $bucket->{amount} = 0; }
27+
sub is_full ($bucket) { $bucket->{amount} == $bucket->{size}; }
28+
sub is_empty ($bucket) { $bucket->{amount} == 0; }
29+
30+
sub pour (%buckets) {
31+
my $quantity = min $buckets{from}{amount}, ( $buckets{into}{size} - $buckets{into}{amount} );
32+
$buckets{from}{amount} -= $quantity;
33+
$buckets{into}{amount} += $quantity;
34+
}
35+
36+
sub result ( $winner, $loser, $moves ) {
37+
return {
38+
moves => $moves,
39+
goalBucket => $winner->{name},
40+
otherBucket => $loser->{amount},
41+
};
42+
}
43+
44+
sub measure ( $bucketOne, $bucketTwo, $goal, $startBucket ) {
45+
validate $bucketOne, $bucketTwo, $goal;
46+
47+
my $first = { name => 'one', size => $bucketOne, amount => 0 };
48+
my $second = { name => 'two', size => $bucketTwo, amount => 0 };
49+
( $first, $second ) = ( $second, $first ) if $startBucket eq 'two';
50+
51+
my $moves = 0;
52+
fill $first;
53+
$moves++;
54+
55+
if ( $second->{size} == $goal && $first->{size} != $goal ) {
56+
fill $second;
57+
$moves++;
58+
}
59+
60+
while (1) {
61+
return result( $first, $second, $moves ) if $first->{amount} == $goal;
62+
return result( $second, $first, $moves ) if $second->{amount} == $goal;
63+
64+
if ( is_empty $first ) { fill $first; }
65+
elsif ( is_full $second ) { empty $second; }
66+
else { pour from => $first, into => $second; }
67+
$moves++;
68+
}
69+
}
70+
71+
1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../t/two-bucket.t
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
subs: measure
2+
properties:
3+
measure:
4+
test: |-
5+
use Data::Dmp;
6+
if (exists $case->{expected}{error}) {
7+
sprintf(<<'END', @{$case->{input}}{qw<bucketOne bucketTwo goal>}, dmp($case->{input}{startBucket}), $case->{expected}{error}, dmp($case->{description}));
8+
like(
9+
dies { measure(%s, %s, %s, %s) },
10+
qr/%s/,
11+
%s
12+
);
13+
END
14+
}
15+
else {
16+
sprintf(<<'END', @{$case->{input}}{qw<bucketOne bucketTwo goal>}, map {dmp $_} ($case->{input}{startBucket}, $case->{expected}, $case->{description}));
17+
is(
18+
measure(%s, %s, %s, %s),
19+
%s,
20+
%s
21+
);
22+
END
23+
}
24+
25+
stub: |-
26+
sub measure ($bucket_1, $bucket_2, $goal, $start_bucket) {
27+
return undef;
28+
}
29+
30+
example: |-
31+
use List::Util qw(max min);
32+
33+
sub gcd ($a, $b) {
34+
while ($b > 0) {
35+
($a, $b) = ($b, $a % $b);
36+
}
37+
return $a;
38+
}
39+
40+
sub validate ($b1, $b2, $goal) {
41+
die 'impossible: goal too big' if $goal > max($b1, $b2);
42+
my $g = gcd $b1, $b2;
43+
die 'impossible: goal unsatisfiable' if $g > 1 && $goal % $g != 0;
44+
}
45+
46+
sub fill ($bucket) { $bucket->{amount} = $bucket->{size}; }
47+
sub empty ($bucket) { $bucket->{amount} = 0; }
48+
sub is_full ($bucket) { $bucket->{amount} == $bucket->{size}; }
49+
sub is_empty ($bucket) { $bucket->{amount} == 0; }
50+
sub pour (%buckets) {
51+
my $quantity = min $buckets{from}{amount}, ($buckets{into}{size} - $buckets{into}{amount});
52+
$buckets{from}{amount} -= $quantity;
53+
$buckets{into}{amount} += $quantity;
54+
}
55+
56+
sub result ($winner, $loser, $moves) {
57+
return {
58+
moves => $moves,
59+
goalBucket => $winner->{name},
60+
otherBucket => $loser->{amount},
61+
};
62+
}
63+
64+
sub measure ($bucketOne, $bucketTwo, $goal, $startBucket) {
65+
validate $bucketOne, $bucketTwo, $goal;
66+
67+
my $first = {name => 'one', size => $bucketOne, amount => 0};
68+
my $second = {name => 'two', size => $bucketTwo, amount => 0};
69+
($first, $second) = ($second, $first) if $startBucket eq 'two';
70+
71+
my $moves = 0;
72+
fill $first;
73+
$moves++;
74+
75+
if ($second->{size} == $goal && $first->{size} != $goal) {
76+
fill $second;
77+
$moves++;
78+
}
79+
80+
while (1) {
81+
return result($first, $second, $moves) if $first->{amount} == $goal;
82+
return result($second, $first, $moves) if $second->{amount} == $goal;
83+
84+
if (is_empty $first) { fill $first; }
85+
elsif (is_full $second) { empty $second; }
86+
else { pour from => $first, into => $second; }
87+
$moves++;
88+
}
89+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[a6f2b4ba-065f-4dca-b6f0-e3eee51cb661]
13+
description = "Measure using bucket one of size 3 and bucket two of size 5 - start with bucket one"
14+
15+
[6c4ea451-9678-4926-b9b3-68364e066d40]
16+
description = "Measure using bucket one of size 3 and bucket two of size 5 - start with bucket two"
17+
18+
[3389f45e-6a56-46d5-9607-75aa930502ff]
19+
description = "Measure using bucket one of size 7 and bucket two of size 11 - start with bucket one"
20+
21+
[fe0ff9a0-3ea5-4bf7-b17d-6d4243961aa1]
22+
description = "Measure using bucket one of size 7 and bucket two of size 11 - start with bucket two"
23+
24+
[0ee1f57e-da84-44f7-ac91-38b878691602]
25+
description = "Measure one step using bucket one of size 1 and bucket two of size 3 - start with bucket two"
26+
27+
[eb329c63-5540-4735-b30b-97f7f4df0f84]
28+
description = "Measure using bucket one of size 2 and bucket two of size 3 - start with bucket one and end with bucket two"
29+
30+
[449be72d-b10a-4f4b-a959-ca741e333b72]
31+
description = "Not possible to reach the goal"
32+
33+
[aac38b7a-77f4-4d62-9b91-8846d533b054]
34+
description = "With the same buckets but a different goal, then it is possible"
35+
36+
[74633132-0ccf-49de-8450-af4ab2e3b299]
37+
description = "Goal larger than both buckets is impossible"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package TwoBucket;
2+
3+
use v5.40;
4+
5+
use Exporter qw<import>;
6+
our @EXPORT_OK = qw<measure>;
7+
8+
sub measure ( $bucket_1, $bucket_2, $goal, $start_bucket ) {
9+
return undef;
10+
}
11+
12+
1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env perl
2+
use Test2::V0;
3+
4+
use FindBin qw<$Bin>;
5+
use lib "$Bin/../lib", "$Bin/../local/lib/perl5";
6+
7+
use TwoBucket qw<measure>;
8+
9+
is( # begin: a6f2b4ba-065f-4dca-b6f0-e3eee51cb661
10+
measure( 3, 5, 1, "one" ),
11+
{ goalBucket => "one", moves => 4, otherBucket => 5 },
12+
"Measure using bucket one of size 3 and bucket two of size 5 - start with bucket one"
13+
); # end: a6f2b4ba-065f-4dca-b6f0-e3eee51cb661
14+
15+
is( # begin: 6c4ea451-9678-4926-b9b3-68364e066d40
16+
measure( 3, 5, 1, "two" ),
17+
{ goalBucket => "two", moves => 8, otherBucket => 3 },
18+
"Measure using bucket one of size 3 and bucket two of size 5 - start with bucket two"
19+
); # end: 6c4ea451-9678-4926-b9b3-68364e066d40
20+
21+
is( # begin: 3389f45e-6a56-46d5-9607-75aa930502ff
22+
measure( 7, 11, 2, "one" ),
23+
{ goalBucket => "one", moves => 14, otherBucket => 11 },
24+
"Measure using bucket one of size 7 and bucket two of size 11 - start with bucket one"
25+
); # end: 3389f45e-6a56-46d5-9607-75aa930502ff
26+
27+
is( # begin: fe0ff9a0-3ea5-4bf7-b17d-6d4243961aa1
28+
measure( 7, 11, 2, "two" ),
29+
{ goalBucket => "two", moves => 18, otherBucket => 7 },
30+
"Measure using bucket one of size 7 and bucket two of size 11 - start with bucket two"
31+
); # end: fe0ff9a0-3ea5-4bf7-b17d-6d4243961aa1
32+
33+
is( # begin: 0ee1f57e-da84-44f7-ac91-38b878691602
34+
measure( 1, 3, 3, "two" ),
35+
{ goalBucket => "two", moves => 1, otherBucket => 0 },
36+
"Measure one step using bucket one of size 1 and bucket two of size 3 - start with bucket two"
37+
); # end: 0ee1f57e-da84-44f7-ac91-38b878691602
38+
39+
is( # begin: eb329c63-5540-4735-b30b-97f7f4df0f84
40+
measure( 2, 3, 3, "one" ),
41+
{ goalBucket => "two", moves => 2, otherBucket => 2 },
42+
"Measure using bucket one of size 2 and bucket two of size 3 - start with bucket one and end with bucket two"
43+
); # end: eb329c63-5540-4735-b30b-97f7f4df0f84
44+
45+
like( # begin: 449be72d-b10a-4f4b-a959-ca741e333b72
46+
dies { measure( 6, 15, 5, "one" ) },
47+
qr/impossible/,
48+
"Not possible to reach the goal"
49+
); # end: 449be72d-b10a-4f4b-a959-ca741e333b72
50+
51+
is( # begin: aac38b7a-77f4-4d62-9b91-8846d533b054
52+
measure( 6, 15, 9, "one" ),
53+
{ goalBucket => "two", moves => 10, otherBucket => 0 },
54+
"With the same buckets but a different goal, then it is possible"
55+
); # end: aac38b7a-77f4-4d62-9b91-8846d533b054
56+
57+
like( # begin: 74633132-0ccf-49de-8450-af4ab2e3b299
58+
dies { measure( 5, 7, 8, "one" ) },
59+
qr/impossible/,
60+
"Goal larger than both buckets is impossible"
61+
); # end: 74633132-0ccf-49de-8450-af4ab2e3b299
62+
63+
done_testing;

0 commit comments

Comments
 (0)