Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "two-bucket",
"name": "Two Bucket",
"uuid": "d6d79e07-fd43-45d0-b3f6-dbb47193c8dd",
"practices": [],
"prerequisites": [],
"difficulty": 4
}
]
},
Expand Down
5 changes: 5 additions & 0 deletions exercises/practice/two-bucket/.busted
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
return {
default = {
ROOT = { '.' }
}
}
46 changes: 46 additions & 0 deletions exercises/practice/two-bucket/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -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/
19 changes: 19 additions & 0 deletions exercises/practice/two-bucket/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"glennj"
],
"files": {
"solution": [
"two_bucket.moon"
],
"test": [
"two_bucket_spec.moon"
],
"example": [
".meta/example.moon"
]
},
"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/"
}
52 changes: 52 additions & 0 deletions exercises/practice/two-bucket/.meta/example.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class Bucket
new: (@name, @size, @amount = 0) =>

is_full: => @amount == @size
is_empty: => @amount == 0
fill: => @amount = @size
empty: => @amount = 0
capacity: => @size - @amount
pour_into: (other) =>
qty = math.min @amount, other\capacity!
@amount -= qty
other.amount += qty


gcd = (a, b) ->
while b != 0
a, b = b, a % b
return a


validate = (b1, b2, goal) ->
assert (goal <= math.max b1, b2), 'goal impossible: too big'

div = gcd b1, b2
assert (div == 1 or goal % div == 0), 'goal impossible: unattainable'


{
measure: (params) ->
validate params.bucketOne, params.bucketTwo, params.goal

goal = params.goal
b1 = Bucket 'one', params.bucketOne
b2 = Bucket 'two', params.bucketTwo
b1, b2 = b2, b1 if params.startBucket == 'two'

b1\fill!
moves = 1

if b2.size == goal and b1.size != goal
b2\fill!
moves += 1

while true
return {:moves, goalBucket: b1.name, otherBucket: b2.amount} if b1.amount == goal
return {:moves, goalBucket: b2.name, otherBucket: b1.amount} if b2.amount == goal

if b1\is_empty! then b1\fill!
elseif b2\is_full! then b2\empty!
else b1\pour_into b2
moves += 1
}
19 changes: 19 additions & 0 deletions exercises/practice/two-bucket/.meta/spec_generator.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
module_imports: {'measure'},

generate_test: (case, level) ->
local lines
cmd = "measure bucketOne: #{case.input.bucketOne}, bucketTwo: #{case.input.bucketTwo}, goal: #{case.input.goal}, startBucket: '#{case.input.startBucket}'"

if case.expected.error
lines = {
"assert.has.errors -> #{cmd}"
}
else
lines = {
"result = #{cmd}",
"expected = moves: #{case.expected.moves}, goalBucket: '#{case.expected.goalBucket}', otherBucket: #{case.expected.otherBucket}",
"assert.are.same expected, result"
}
table.concat [indent line, level for line in *lines], '\n'
}
43 changes: 43 additions & 0 deletions exercises/practice/two-bucket/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 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"

[58d70152-bf2b-46bb-ad54-be58ebe94c03]
description = "Measure using bucket one much bigger than bucket two"

[9dbe6499-caa5-4a58-b5ce-c988d71b8981]
description = "Measure using bucket one much smaller than 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"
4 changes: 4 additions & 0 deletions exercises/practice/two-bucket/two_bucket.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
measure: (params) ->
error 'Implement me'
}
53 changes: 53 additions & 0 deletions exercises/practice/two-bucket/two_bucket_spec.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import measure from require 'two_bucket'

describe 'two-bucket', ->
it 'Measure using bucket one of size 3 and bucket two of size 5 - start with bucket one', ->
result = measure bucketOne: 3, bucketTwo: 5, goal: 1, startBucket: 'one'
expected = moves: 4, goalBucket: 'one', otherBucket: 5
assert.are.same expected, result

pending 'Measure using bucket one of size 3 and bucket two of size 5 - start with bucket two', ->
result = measure bucketOne: 3, bucketTwo: 5, goal: 1, startBucket: 'two'
expected = moves: 8, goalBucket: 'two', otherBucket: 3
assert.are.same expected, result

pending 'Measure using bucket one of size 7 and bucket two of size 11 - start with bucket one', ->
result = measure bucketOne: 7, bucketTwo: 11, goal: 2, startBucket: 'one'
expected = moves: 14, goalBucket: 'one', otherBucket: 11
assert.are.same expected, result

pending 'Measure using bucket one of size 7 and bucket two of size 11 - start with bucket two', ->
result = measure bucketOne: 7, bucketTwo: 11, goal: 2, startBucket: 'two'
expected = moves: 18, goalBucket: 'two', otherBucket: 7
assert.are.same expected, result

pending 'Measure one step using bucket one of size 1 and bucket two of size 3 - start with bucket two', ->
result = measure bucketOne: 1, bucketTwo: 3, goal: 3, startBucket: 'two'
expected = moves: 1, goalBucket: 'two', otherBucket: 0
assert.are.same expected, result

pending 'Measure using bucket one of size 2 and bucket two of size 3 - start with bucket one and end with bucket two', ->
result = measure bucketOne: 2, bucketTwo: 3, goal: 3, startBucket: 'one'
expected = moves: 2, goalBucket: 'two', otherBucket: 2
assert.are.same expected, result

pending 'Measure using bucket one much bigger than bucket two', ->
result = measure bucketOne: 5, bucketTwo: 1, goal: 2, startBucket: 'one'
expected = moves: 6, goalBucket: 'one', otherBucket: 1
assert.are.same expected, result

pending 'Measure using bucket one much smaller than bucket two', ->
result = measure bucketOne: 3, bucketTwo: 15, goal: 9, startBucket: 'one'
expected = moves: 6, goalBucket: 'two', otherBucket: 0
assert.are.same expected, result

pending 'Not possible to reach the goal', ->
assert.has.errors -> measure bucketOne: 6, bucketTwo: 15, goal: 5, startBucket: 'one'

pending 'With the same buckets but a different goal, then it is possible', ->
result = measure bucketOne: 6, bucketTwo: 15, goal: 9, startBucket: 'one'
expected = moves: 10, goalBucket: 'two', otherBucket: 0
assert.are.same expected, result

pending 'Goal larger than both buckets is impossible', ->
assert.has.errors -> measure bucketOne: 5, bucketTwo: 7, goal: 8, startBucket: 'one'
Loading