diff --git a/config.json b/config.json index d02116c8..dc440ded 100644 --- a/config.json +++ b/config.json @@ -1187,6 +1187,17 @@ "strings" ] }, + { + "slug": "two-bucket", + "name": "Two Bucket", + "uuid": "c8e85e85-2e3e-47ab-98f2-21eaaa12d98e", + "practices": [], + "prerequisites": [], + "difficulty": 6, + "topics": [ + "control_flow_loops" + ] + }, { "slug": "tournament", "name": "Tournament", diff --git a/exercises/practice/two-bucket/.busted b/exercises/practice/two-bucket/.busted new file mode 100644 index 00000000..86b84e7c --- /dev/null +++ b/exercises/practice/two-bucket/.busted @@ -0,0 +1,5 @@ +return { + default = { + ROOT = { '.' } + } +} diff --git a/exercises/practice/two-bucket/.docs/instructions.md b/exercises/practice/two-bucket/.docs/instructions.md new file mode 100644 index 00000000..7249deb3 --- /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 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..4291f872 --- /dev/null +++ b/exercises/practice/two-bucket/.meta/config.json @@ -0,0 +1,17 @@ +{ + "authors": [], + "files": { + "solution": [ + "two-bucket.lua" + ], + "test": [ + "two-bucket_spec.lua" + ], + "example": [ + ".meta/example.lua" + ] + }, + "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/example.lua b/exercises/practice/two-bucket/.meta/example.lua new file mode 100644 index 00000000..00dc2800 --- /dev/null +++ b/exercises/practice/two-bucket/.meta/example.lua @@ -0,0 +1,79 @@ +local function Bucket(capacity) + local self = { + volume = 0, + capacity = capacity + } + + self.empty = function() + self.volume = 0 + end + + self.is_empty = function() + return self.volume == 0 + end + + self.is_full = function() + return self.volume == self.capacity + end + + self.fill = function() + self.volume = self.capacity + end + + self.pour_into = function(other) + local amount = math.min(self.volume, other.remaining()) + self.volume = self.volume - amount + other.volume = other.volume + amount + end + + self.remaining = function() + return self.capacity - self.volume + end + + self.contains = function(amount) + return self.volume == amount + end + + return self +end + +return { + measure = function(args) + local bucket_one = Bucket(args.bucket_one_capacity) + local bucket_two = Bucket(args.bucket_two_capacity) + local goal_volume = args.goal_volume + local start_bucket = args.start_bucket + + local start = start_bucket == 1 and bucket_one or bucket_two + local other = start_bucket == 2 and bucket_one or bucket_two + + local previous_states = {} + + local moves = 1 + start.fill() + + while not (start.contains(goal_volume) or other.contains(goal_volume)) do + local current_state = tostring(start.volume) .. ':' .. tostring(other.volume) + assert(not previous_states[current_state]) + previous_states[current_state] = true + + moves = moves + 1 + + if other.capacity == goal_volume then + other.fill() + elseif other.is_full() then + other.empty() + elseif not start.is_empty() then + start.pour_into(other) + else + start.fill() + end + end + + return { + moves = moves, + other_bucket_volume = bucket_one.contains(goal_volume) and bucket_two.volume or bucket_one.volume, + goal_bucket_volume = bucket_one.contains(goal_volume) and 1 or 2 + } + end +} 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/two-bucket.lua b/exercises/practice/two-bucket/two-bucket.lua new file mode 100644 index 00000000..c53ff44e --- /dev/null +++ b/exercises/practice/two-bucket/two-bucket.lua @@ -0,0 +1,5 @@ +return { + measure = function(args) + + end +} diff --git a/exercises/practice/two-bucket/two-bucket_spec.lua b/exercises/practice/two-bucket/two-bucket_spec.lua new file mode 100644 index 00000000..76c7b7ca --- /dev/null +++ b/exercises/practice/two-bucket/two-bucket_spec.lua @@ -0,0 +1,116 @@ +local two_bucket = require 'two-bucket' + +describe('two-bucket', function() + it('measure using bucket one of size 3 and bucket two of size 5 - start with bucket one', function() + assert.are.same(two_bucket.measure({ + bucket_one_capacity = 3, + bucket_two_capacity = 5, + goal_volume = 1, + start_bucket = 1 + }), { + moves = 4, + other_bucket_volume = 5, + goal_bucket_volume = 1 + }) + end) + + it('measure using bucket one of size 3 and bucket two of size 5 - start with bucket two', function() + assert.are.same(two_bucket.measure({ + bucket_one_capacity = 3, + bucket_two_capacity = 5, + goal_volume = 1, + start_bucket = 2 + }), { + moves = 8, + other_bucket_volume = 3, + goal_bucket_volume = 2 + }) + end) + + it('measure using bucket one of size 7 and bucket two of size 11 - start with bucket one', function() + assert.are.same(two_bucket.measure({ + bucket_one_capacity = 7, + bucket_two_capacity = 11, + goal_volume = 2, + start_bucket = 1 + }), { + moves = 14, + other_bucket_volume = 11, + goal_bucket_volume = 1 + }) + end) + + it('measure using bucket one of size 7 and bucket two of size 11 - start with bucket two', function() + assert.are.same(two_bucket.measure({ + bucket_one_capacity = 7, + bucket_two_capacity = 11, + goal_volume = 2, + start_bucket = 2 + }), { + moves = 18, + other_bucket_volume = 7, + goal_bucket_volume = 2 + }) + end) + + it('measure one step using bucket one of size 1 and bucket two of size 3 - start with bucket two', function() + assert.are.same(two_bucket.measure({ + bucket_one_capacity = 1, + bucket_two_capacity = 3, + goal_volume = 3, + start_bucket = 2 + }), { + moves = 1, + other_bucket_volume = 0, + goal_bucket_volume = 2 + }) + end) + + it('measure using bucket one of size 2 and bucket two of size 3 - start with bucket one and end with bucket two', function() + assert.are.same(two_bucket.measure({ + bucket_one_capacity = 2, + bucket_two_capacity = 3, + goal_volume = 3, + start_bucket = 1 + }), { + moves = 2, + other_bucket_volume = 2, + goal_bucket_volume = 2 + }) + end) + + it('not possible to reach the goal', function() + assert.has_error(function() + two_bucket.measure({ + bucket_one_capacity = 6, + bucket_two_capacity = 15, + goal_volume = 5, + start_bucket = 1 + }) + end) + end) + + it('with the same buckets but a different goal, then it is possible', function() + assert.are.same(two_bucket.measure({ + bucket_one_capacity = 6, + bucket_two_capacity = 15, + goal_volume = 9, + start_bucket = 1 + }), { + moves = 10, + other_bucket_volume = 0, + goal_bucket_volume = 2 + }) + end) + + it('goal larger than both buckets is impossible', function() + assert.has_error(function() + two_bucket.measure({ + bucket_one_capacity = 5, + bucket_two_capacity = 7, + goal_volume = 8, + start_bucket = 1 + }) + end) + end) +end)