Skip to content

Commit e0063c8

Browse files
committed
two-bucket
1 parent a2d9070 commit e0063c8

File tree

9 files changed

+249
-0
lines changed

9 files changed

+249
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,14 @@
414414
"practices": [],
415415
"prerequisites": [],
416416
"difficulty": 4
417+
},
418+
{
419+
"slug": "two-bucket",
420+
"name": "Two Bucket",
421+
"uuid": "d6d79e07-fd43-45d0-b3f6-dbb47193c8dd",
422+
"practices": [],
423+
"prerequisites": [],
424+
"difficulty": 4
417425
}
418426
]
419427
},
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
return {
2+
default = {
3+
ROOT = { '.' }
4+
}
5+
}
Lines changed: 46 additions & 0 deletions
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/
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"glennj"
4+
],
5+
"files": {
6+
"solution": [
7+
"two_bucket.moon"
8+
],
9+
"test": [
10+
"two_bucket_spec.moon"
11+
],
12+
"example": [
13+
".meta/example.moon"
14+
]
15+
},
16+
"blurb": "Given two buckets of different size, demonstrate how to measure an exact number of liters.",
17+
"source": "Water Pouring Problem",
18+
"source_url": "https://demonstrations.wolfram.com/WaterPouringProblem/"
19+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class Bucket
2+
new: (@name, @size, @amount = 0) =>
3+
4+
is_full: => @amount == @size
5+
is_empty: => @amount == 0
6+
fill: => @amount = @size
7+
empty: => @amount = 0
8+
capacity: => @size - @amount
9+
pour_into: (other) =>
10+
qty = math.min @amount, other\capacity!
11+
@amount -= qty
12+
other.amount += qty
13+
14+
15+
gcd = (a, b) ->
16+
while b != 0
17+
a, b = b, a % b
18+
return a
19+
20+
21+
validate = (b1, b2, goal) ->
22+
assert (goal <= math.max b1, b2), 'goal impossible: too big'
23+
24+
div = gcd b1, b2
25+
assert (div == 1 or goal % div == 0), 'goal impossible: unattainable'
26+
27+
28+
{
29+
measure: (params) ->
30+
validate params.bucketOne, params.bucketTwo, params.goal
31+
32+
goal = params.goal
33+
b1 = Bucket 'one', params.bucketOne
34+
b2 = Bucket 'two', params.bucketTwo
35+
b1, b2 = b2, b1 if params.startBucket == 'two'
36+
37+
b1\fill!
38+
moves = 1
39+
40+
if b2.size == goal and b1.size != goal
41+
b2\fill!
42+
moves += 1
43+
44+
while true
45+
return {:moves, goalBucket: b1.name, otherBucket: b2.amount} if b1.amount == goal
46+
return {:moves, goalBucket: b2.name, otherBucket: b1.amount} if b2.amount == goal
47+
48+
if b1\is_empty! then b1\fill!
49+
elseif b2\is_full! then b2\empty!
50+
else b1\pour_into b2
51+
moves += 1
52+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
module_imports: {'measure'},
3+
4+
generate_test: (case, level) ->
5+
local lines
6+
cmd = "measure bucketOne: #{case.input.bucketOne}, bucketTwo: #{case.input.bucketTwo}, goal: #{case.input.goal}, startBucket: '#{case.input.startBucket}'"
7+
8+
if case.expected.error
9+
lines = {
10+
"assert.has.errors -> #{cmd}"
11+
}
12+
else
13+
lines = {
14+
"result = #{cmd}",
15+
"expected = moves: #{case.expected.moves}, goalBucket: '#{case.expected.goalBucket}', otherBucket: #{case.expected.otherBucket}",
16+
"assert.are.same expected, result"
17+
}
18+
table.concat [indent line, level for line in *lines], '\n'
19+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
[58d70152-bf2b-46bb-ad54-be58ebe94c03]
31+
description = "Measure using bucket one much bigger than bucket two"
32+
33+
[9dbe6499-caa5-4a58-b5ce-c988d71b8981]
34+
description = "Measure using bucket one much smaller than bucket two"
35+
36+
[449be72d-b10a-4f4b-a959-ca741e333b72]
37+
description = "Not possible to reach the goal"
38+
39+
[aac38b7a-77f4-4d62-9b91-8846d533b054]
40+
description = "With the same buckets but a different goal, then it is possible"
41+
42+
[74633132-0ccf-49de-8450-af4ab2e3b299]
43+
description = "Goal larger than both buckets is impossible"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
measure: (params) ->
3+
error 'Implement me'
4+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import measure from require 'two_bucket'
2+
3+
describe 'two-bucket', ->
4+
it 'Measure using bucket one of size 3 and bucket two of size 5 - start with bucket one', ->
5+
result = measure bucketOne: 3, bucketTwo: 5, goal: 1, startBucket: 'one'
6+
expected = moves: 4, goalBucket: 'one', otherBucket: 5
7+
assert.are.same expected, result
8+
9+
pending 'Measure using bucket one of size 3 and bucket two of size 5 - start with bucket two', ->
10+
result = measure bucketOne: 3, bucketTwo: 5, goal: 1, startBucket: 'two'
11+
expected = moves: 8, goalBucket: 'two', otherBucket: 3
12+
assert.are.same expected, result
13+
14+
pending 'Measure using bucket one of size 7 and bucket two of size 11 - start with bucket one', ->
15+
result = measure bucketOne: 7, bucketTwo: 11, goal: 2, startBucket: 'one'
16+
expected = moves: 14, goalBucket: 'one', otherBucket: 11
17+
assert.are.same expected, result
18+
19+
pending 'Measure using bucket one of size 7 and bucket two of size 11 - start with bucket two', ->
20+
result = measure bucketOne: 7, bucketTwo: 11, goal: 2, startBucket: 'two'
21+
expected = moves: 18, goalBucket: 'two', otherBucket: 7
22+
assert.are.same expected, result
23+
24+
pending 'Measure one step using bucket one of size 1 and bucket two of size 3 - start with bucket two', ->
25+
result = measure bucketOne: 1, bucketTwo: 3, goal: 3, startBucket: 'two'
26+
expected = moves: 1, goalBucket: 'two', otherBucket: 0
27+
assert.are.same expected, result
28+
29+
pending 'Measure using bucket one of size 2 and bucket two of size 3 - start with bucket one and end with bucket two', ->
30+
result = measure bucketOne: 2, bucketTwo: 3, goal: 3, startBucket: 'one'
31+
expected = moves: 2, goalBucket: 'two', otherBucket: 2
32+
assert.are.same expected, result
33+
34+
pending 'Measure using bucket one much bigger than bucket two', ->
35+
result = measure bucketOne: 5, bucketTwo: 1, goal: 2, startBucket: 'one'
36+
expected = moves: 6, goalBucket: 'one', otherBucket: 1
37+
assert.are.same expected, result
38+
39+
pending 'Measure using bucket one much smaller than bucket two', ->
40+
result = measure bucketOne: 3, bucketTwo: 15, goal: 9, startBucket: 'one'
41+
expected = moves: 6, goalBucket: 'two', otherBucket: 0
42+
assert.are.same expected, result
43+
44+
pending 'Not possible to reach the goal', ->
45+
assert.has.errors -> measure bucketOne: 6, bucketTwo: 15, goal: 5, startBucket: 'one'
46+
47+
pending 'With the same buckets but a different goal, then it is possible', ->
48+
result = measure bucketOne: 6, bucketTwo: 15, goal: 9, startBucket: 'one'
49+
expected = moves: 10, goalBucket: 'two', otherBucket: 0
50+
assert.are.same expected, result
51+
52+
pending 'Goal larger than both buckets is impossible', ->
53+
assert.has.errors -> measure bucketOne: 5, bucketTwo: 7, goal: 8, startBucket: 'one'

0 commit comments

Comments
 (0)