From f08d871dad7aa89883e54fa06c9b291e4e6e0be5 Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Tue, 12 Nov 2024 09:44:41 -0500 Subject: [PATCH] Add two-bucket exercise --- config.json | 8 ++ .../practice/two-bucket/.docs/instructions.md | 46 ++++++++++ .../practice/two-bucket/.meta/config.json | 19 ++++ .../.meta/solutions/lib/TwoBucket.pm | 72 +++++++++++++++ .../two-bucket/.meta/solutions/t/two-bucket.t | 1 + .../two-bucket/.meta/template-data.yaml | 90 +++++++++++++++++++ .../practice/two-bucket/.meta/tests.toml | 37 ++++++++ .../practice/two-bucket/lib/TwoBucket.pm | 12 +++ exercises/practice/two-bucket/t/two-bucket.t | 63 +++++++++++++ 9 files changed, 348 insertions(+) create mode 100644 exercises/practice/two-bucket/.docs/instructions.md create mode 100644 exercises/practice/two-bucket/.meta/config.json create mode 100644 exercises/practice/two-bucket/.meta/solutions/lib/TwoBucket.pm create mode 120000 exercises/practice/two-bucket/.meta/solutions/t/two-bucket.t create mode 100644 exercises/practice/two-bucket/.meta/template-data.yaml create mode 100644 exercises/practice/two-bucket/.meta/tests.toml create mode 100644 exercises/practice/two-bucket/lib/TwoBucket.pm create mode 100755 exercises/practice/two-bucket/t/two-bucket.t diff --git a/config.json b/config.json index 80564110..f9f848b2 100644 --- a/config.json +++ b/config.json @@ -719,6 +719,14 @@ "prerequisites": [], "difficulty": 1 }, + { + "slug": "two-bucket", + "name": "Two Bucket", + "uuid": "1ac50ef0-d6c8-4217-867f-3a72a353ac93", + "practices": [], + "prerequisites": [], + "difficulty": 4 + }, { "slug": "two-fer", "name": "Two Fer", diff --git a/exercises/practice/two-bucket/.docs/instructions.md b/exercises/practice/two-bucket/.docs/instructions.md new file mode 100644 index 00000000..30d779aa --- /dev/null +++ b/exercises/practice/two-bucket/.docs/instructions.md @@ -0,0 +1,46 @@ +# Instructions + +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. + +There are some rules that your solution must follow: + +- You can only do one action at a time. +- There are only 3 possible actions: + 1. Pouring one bucket into the other bucket until either: + a) the first bucket is empty + b) the second bucket is full + 2. Emptying a bucket and doing nothing to the other. + 3. Filling a bucket and doing nothing to the other. +- After an action, you may not arrive at a state where the initial starting bucket is empty and the other bucket is full. + +Your program will take as input: + +- the size of bucket one +- the size of bucket two +- the desired number of liters to reach +- which bucket to fill first, either bucket one or bucket two + +Your program should determine: + +- the total number of actions it should take to reach the desired number of liters, including the first fill of the starting bucket +- which bucket should end up with the desired number of liters - either bucket one or bucket two +- how many liters are left in the other bucket + +Note: any time a change is made to either or both buckets counts as one (1) action. + +Example: +Bucket one can hold up to 7 liters, and bucket two can hold up to 11 liters. +Let's say at a given step, bucket one is holding 7 liters and bucket two is holding 8 liters (7,8). +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. +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. + +Another Example: +Bucket one can hold 3 liters, and bucket two can hold up to 5 liters. +You are told you must start with bucket one. +So your first action is to fill bucket one. +You choose to empty bucket one for your second action. +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. + +Written with <3 at [Fullstack Academy][fullstack] by Lindsay Levine. + +[fullstack]: https://www.fullstackacademy.com/ diff --git a/exercises/practice/two-bucket/.meta/config.json b/exercises/practice/two-bucket/.meta/config.json new file mode 100644 index 00000000..824b7363 --- /dev/null +++ b/exercises/practice/two-bucket/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "glennj" + ], + "files": { + "solution": [ + "lib/TwoBucket.pm" + ], + "test": [ + "t/two-bucket.t" + ], + "example": [ + ".meta/solutions/lib/TwoBucket.pm" + ] + }, + "blurb": "Given two buckets of different size, demonstrate how to measure an exact number of liters.", + "source": "Water Pouring Problem", + "source_url": "https://demonstrations.wolfram.com/WaterPouringProblem/" +} diff --git a/exercises/practice/two-bucket/.meta/solutions/lib/TwoBucket.pm b/exercises/practice/two-bucket/.meta/solutions/lib/TwoBucket.pm new file mode 100644 index 00000000..59099f4f --- /dev/null +++ b/exercises/practice/two-bucket/.meta/solutions/lib/TwoBucket.pm @@ -0,0 +1,72 @@ +package TwoBucket; + +use strict; +use warnings; +use experimental qw; + +use Exporter qw; +our @EXPORT_OK = qw; + +use List::Util qw(max min); + +sub gcd ( $a, $b ) { + while ( $b > 0 ) { + ( $a, $b ) = ( $b, $a % $b ); + } + return $a; +} + +sub validate ( $b1, $b2, $goal ) { + die 'impossible: goal too big' if $goal > max( $b1, $b2 ); + my $g = gcd $b1, $b2; + die 'impossible: goal unsatisfiable' if $g > 1 && $goal % $g != 0; +} + +sub fill ($bucket) { $bucket->{amount} = $bucket->{size}; } +sub empty ($bucket) { $bucket->{amount} = 0; } +sub is_full ($bucket) { $bucket->{amount} == $bucket->{size}; } +sub is_empty ($bucket) { $bucket->{amount} == 0; } + +sub pour (%buckets) { + my $quantity = min $buckets{from}{amount}, ( $buckets{into}{size} - $buckets{into}{amount} ); + $buckets{from}{amount} -= $quantity; + $buckets{into}{amount} += $quantity; +} + +sub result ( $winner, $loser, $moves ) { + return { + moves => $moves, + goalBucket => $winner->{name}, + otherBucket => $loser->{amount}, + }; +} + +sub measure ( $bucketOne, $bucketTwo, $goal, $startBucket ) { + validate $bucketOne, $bucketTwo, $goal; + + my $first = { name => 'one', size => $bucketOne, amount => 0 }; + my $second = { name => 'two', size => $bucketTwo, amount => 0 }; + ( $first, $second ) = ( $second, $first ) if $startBucket eq 'two'; + + my $moves = 0; + + fill $first; + $moves++; + + if ( $second->{size} == $goal ) { + fill $second; + $moves++; + } + + while (1) { + return result( $first, $second, $moves ) if $first->{amount} == $goal; + return result( $second, $first, $moves ) if $second->{amount} == $goal; + + if ( is_empty $first ) { fill $first; } + elsif ( is_full $second ) { empty $second; } + else { pour from => $first, into => $second; } + $moves++; + } +} + +1; diff --git a/exercises/practice/two-bucket/.meta/solutions/t/two-bucket.t b/exercises/practice/two-bucket/.meta/solutions/t/two-bucket.t new file mode 120000 index 00000000..8ad1060d --- /dev/null +++ b/exercises/practice/two-bucket/.meta/solutions/t/two-bucket.t @@ -0,0 +1 @@ +../../../t/two-bucket.t \ No newline at end of file diff --git a/exercises/practice/two-bucket/.meta/template-data.yaml b/exercises/practice/two-bucket/.meta/template-data.yaml new file mode 100644 index 00000000..87eba872 --- /dev/null +++ b/exercises/practice/two-bucket/.meta/template-data.yaml @@ -0,0 +1,90 @@ +subs: measure +properties: + measure: + test: |- + use Data::Dmp; + if (exists $case->{expected}{error}) { + sprintf(<<'END', @{$case->{input}}{qw}, dmp($case->{input}{startBucket}), $case->{expected}{error}, dmp($case->{description})); + like( + dies { measure(%s, %s, %s, %s) }, + qr/%s/, + %s + ); + END + } + else { + sprintf(<<'END', @{$case->{input}}{qw}, map {dmp $_} ($case->{input}{startBucket}, $case->{expected}, $case->{description})); + is( + measure(%s, %s, %s, %s), + %s, + %s + ); + END + } + +stub: |- + sub measure ($bucketOne, $bucketTwo, $goal, $startBucket) { + return undef; + } + +example: |- + use List::Util qw(max min); + + sub gcd ($a, $b) { + while ($b > 0) { + ($a, $b) = ($b, $a % $b); + } + return $a; + } + + sub validate ($b1, $b2, $goal) { + die 'impossible: goal too big' if $goal > max($b1, $b2); + my $g = gcd $b1, $b2; + die 'impossible: goal unsatisfiable' if $g > 1 && $goal % $g != 0; + } + + sub fill ($bucket) { $bucket->{amount} = $bucket->{size}; } + sub empty ($bucket) { $bucket->{amount} = 0; } + sub is_full ($bucket) { $bucket->{amount} == $bucket->{size}; } + sub is_empty ($bucket) { $bucket->{amount} == 0; } + sub pour (%buckets) { + my $quantity = min $buckets{from}{amount}, ($buckets{into}{size} - $buckets{into}{amount}); + $buckets{from}{amount} -= $quantity; + $buckets{into}{amount} += $quantity; + } + + sub result ($winner, $loser, $moves) { + return { + moves => $moves, + goalBucket => $winner->{name}, + otherBucket => $loser->{amount}, + }; + } + + sub measure ($bucketOne, $bucketTwo, $goal, $startBucket) { + validate $bucketOne, $bucketTwo, $goal; + + my $first = {name => 'one', size => $bucketOne, amount => 0}; + my $second = {name => 'two', size => $bucketTwo, amount => 0}; + ($first, $second) = ($second, $first) if $startBucket eq 'two'; + + my $moves = 0; + + fill $first; + $moves++; + + if ($second->{size} == $goal) { + fill $second; + $moves++; + } + + while (1) { + return result($first, $second, $moves) if $first->{amount} == $goal; + return result($second, $first, $moves) if $second->{amount} == $goal; + + if (is_empty $first) { fill $first; } + elsif (is_full $second) { empty $second; } + else { pour from => $first, into => $second; } + $moves++; + } + } diff --git a/exercises/practice/two-bucket/.meta/tests.toml b/exercises/practice/two-bucket/.meta/tests.toml new file mode 100644 index 00000000..d6ff02f5 --- /dev/null +++ b/exercises/practice/two-bucket/.meta/tests.toml @@ -0,0 +1,37 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[a6f2b4ba-065f-4dca-b6f0-e3eee51cb661] +description = "Measure using bucket one of size 3 and bucket two of size 5 - start with bucket one" + +[6c4ea451-9678-4926-b9b3-68364e066d40] +description = "Measure using bucket one of size 3 and bucket two of size 5 - start with bucket two" + +[3389f45e-6a56-46d5-9607-75aa930502ff] +description = "Measure using bucket one of size 7 and bucket two of size 11 - start with bucket one" + +[fe0ff9a0-3ea5-4bf7-b17d-6d4243961aa1] +description = "Measure using bucket one of size 7 and bucket two of size 11 - start with bucket two" + +[0ee1f57e-da84-44f7-ac91-38b878691602] +description = "Measure one step using bucket one of size 1 and bucket two of size 3 - start with bucket two" + +[eb329c63-5540-4735-b30b-97f7f4df0f84] +description = "Measure using bucket one of size 2 and bucket two of size 3 - start with bucket one and end with bucket two" + +[449be72d-b10a-4f4b-a959-ca741e333b72] +description = "Not possible to reach the goal" + +[aac38b7a-77f4-4d62-9b91-8846d533b054] +description = "With the same buckets but a different goal, then it is possible" + +[74633132-0ccf-49de-8450-af4ab2e3b299] +description = "Goal larger than both buckets is impossible" diff --git a/exercises/practice/two-bucket/lib/TwoBucket.pm b/exercises/practice/two-bucket/lib/TwoBucket.pm new file mode 100644 index 00000000..37b47b0b --- /dev/null +++ b/exercises/practice/two-bucket/lib/TwoBucket.pm @@ -0,0 +1,12 @@ +package TwoBucket; + +use v5.40; + +use Exporter qw; +our @EXPORT_OK = qw; + +sub measure ( $bucketOne, $bucketTwo, $goal, $startBucket ) { + return undef; +} + +1; diff --git a/exercises/practice/two-bucket/t/two-bucket.t b/exercises/practice/two-bucket/t/two-bucket.t new file mode 100755 index 00000000..d1093953 --- /dev/null +++ b/exercises/practice/two-bucket/t/two-bucket.t @@ -0,0 +1,63 @@ +#!/usr/bin/env perl +use Test2::V0; + +use FindBin qw<$Bin>; +use lib "$Bin/../lib", "$Bin/../local/lib/perl5"; + +use TwoBucket qw; + +is( # begin: a6f2b4ba-065f-4dca-b6f0-e3eee51cb661 + measure( 3, 5, 1, "one" ), + { goalBucket => "one", moves => 4, otherBucket => 5 }, + "Measure using bucket one of size 3 and bucket two of size 5 - start with bucket one" +); # end: a6f2b4ba-065f-4dca-b6f0-e3eee51cb661 + +is( # begin: 6c4ea451-9678-4926-b9b3-68364e066d40 + measure( 3, 5, 1, "two" ), + { goalBucket => "two", moves => 8, otherBucket => 3 }, + "Measure using bucket one of size 3 and bucket two of size 5 - start with bucket two" +); # end: 6c4ea451-9678-4926-b9b3-68364e066d40 + +is( # begin: 3389f45e-6a56-46d5-9607-75aa930502ff + measure( 7, 11, 2, "one" ), + { goalBucket => "one", moves => 14, otherBucket => 11 }, + "Measure using bucket one of size 7 and bucket two of size 11 - start with bucket one" +); # end: 3389f45e-6a56-46d5-9607-75aa930502ff + +is( # begin: fe0ff9a0-3ea5-4bf7-b17d-6d4243961aa1 + measure( 7, 11, 2, "two" ), + { goalBucket => "two", moves => 18, otherBucket => 7 }, + "Measure using bucket one of size 7 and bucket two of size 11 - start with bucket two" +); # end: fe0ff9a0-3ea5-4bf7-b17d-6d4243961aa1 + +is( # begin: 0ee1f57e-da84-44f7-ac91-38b878691602 + measure( 1, 3, 3, "two" ), + { goalBucket => "two", moves => 1, otherBucket => 0 }, + "Measure one step using bucket one of size 1 and bucket two of size 3 - start with bucket two" +); # end: 0ee1f57e-da84-44f7-ac91-38b878691602 + +is( # begin: eb329c63-5540-4735-b30b-97f7f4df0f84 + measure( 2, 3, 3, "one" ), + { goalBucket => "two", moves => 2, otherBucket => 2 }, + "Measure using bucket one of size 2 and bucket two of size 3 - start with bucket one and end with bucket two" +); # end: eb329c63-5540-4735-b30b-97f7f4df0f84 + +like( # begin: 449be72d-b10a-4f4b-a959-ca741e333b72 + dies { measure( 6, 15, 5, "one" ) }, + qr/impossible/, + "Not possible to reach the goal" +); # end: 449be72d-b10a-4f4b-a959-ca741e333b72 + +is( # begin: aac38b7a-77f4-4d62-9b91-8846d533b054 + measure( 6, 15, 9, "one" ), + { goalBucket => "two", moves => 10, otherBucket => 0 }, + "With the same buckets but a different goal, then it is possible" +); # end: aac38b7a-77f4-4d62-9b91-8846d533b054 + +like( # begin: 74633132-0ccf-49de-8450-af4ab2e3b299 + dies { measure( 5, 7, 8, "one" ) }, + qr/impossible/, + "Goal larger than both buckets is impossible" +); # end: 74633132-0ccf-49de-8450-af4ab2e3b299 + +done_testing;