Skip to content

Commit b12f6a9

Browse files
committed
Sync two-bucket with problem-specifications
1 parent 3c6e571 commit b12f6a9

5 files changed

Lines changed: 167 additions & 82 deletions

File tree

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,46 @@
11
# Instructions
22

3-
Given two buckets of different size, demonstrate how to measure an exact number of liters by strategically transferring liters of fluid between the buckets.
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.
44

5-
Since this mathematical problem is fairly subject to interpretation / individual approach, the tests have been written specifically to expect one overarching solution.
5+
There are some rules that your solution must follow:
66

7-
To help, the tests provide you with which bucket to fill first. That means, when starting with the larger bucket full, you are NOT allowed at any point to have the smaller bucket full and the larger bucket empty (aka, the opposite starting point); that would defeat the purpose of comparing both approaches!
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 starting bucket is empty and the other bucket is full.
815

916
Your program will take as input:
17+
1018
- the size of bucket one
1119
- the size of bucket two
1220
- the desired number of liters to reach
1321
- which bucket to fill first, either bucket one or bucket two
1422

1523
Your program should determine:
16-
- the total number of "moves" it should take to reach the desired number of liters, including the first fill
17-
- which bucket should end up with the desired number of liters (let's say this is bucket A) - either bucket one or bucket two
18-
- how many liters are left in the other bucket (bucket B)
1924

20-
Note: any time a change is made to either or both buckets counts as one (1) move.
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.
2130

2231
Example:
23-
Bucket one can hold up to 7 liters, and bucket two can hold up to 11 liters. Let's say bucket one, at a given step, 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 "move". Instead, if you had poured from bucket one into bucket two until bucket two was full, leaving you with 4 liters in bucket one and 11 liters in bucket two (4,11), that would count as only one "move" as well.
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.
2443

25-
To conclude, the only valid moves are:
26-
- pouring from either bucket to another
27-
- emptying either bucket and doing nothing to the other
28-
- filling either bucket and doing nothing to the other
44+
Written with <3 at [Fullstack Academy][fullstack] by Lindsay Levine.
2945

30-
Written with <3 at [Fullstack Academy](http://www.fullstackacademy.com/) by Lindsay Levine.
46+
[fullstack]: https://www.fullstackacademy.com/

exercises/practice/two-bucket/.meta/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@
3030
},
3131
"blurb": "Given two buckets of different size, demonstrate how to measure an exact number of liters.",
3232
"source": "Water Pouring Problem",
33-
"source_url": "http://demonstrations.wolfram.com/WaterPouringProblem/"
33+
"source_url": "https://demonstrations.wolfram.com/WaterPouringProblem/"
3434
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{% macro bucket(label) -%}
2+
{% if label == 'one' -%}
3+
Bucket::One
4+
{%- else -%}
5+
Bucket::Two
6+
{%- endif %}
7+
{%- endmacro bucket -%}
8+
9+
use two_bucket::{Bucket, BucketStats};
10+
{% for test in cases %}
11+
#[test]
12+
{% if loop.index != 1 -%}
13+
#[ignore]
14+
{% endif -%}
15+
fn {{ test.description | slugify | replace(from="-", to="_") }}() {
16+
let output = {{ crate_name }}::{{ fn_names[0] }}(
17+
{{ test.input.bucketOne }}, {{ test.input.bucketTwo }}, {{ test.input.goal }},
18+
&{{ self::bucket(label=test.input.startBucket) }},
19+
);
20+
let expected = {% if 'error' in test.expected -%}
21+
None
22+
{%- else -%}
23+
Some(BucketStats {
24+
moves: {{ test.expected.moves }},
25+
goal_bucket: {{ self::bucket(label=test.expected.goalBucket) }},
26+
other_bucket: {{ test.expected.otherBucket }},
27+
})
28+
{%- endif %};
29+
assert_eq!(output, expected);
30+
}
31+
{% endfor -%}
Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,37 @@
1-
# This is an auto-generated file. Regular comments will be removed when this
2-
# file is regenerated. Regenerating will not touch any manually added keys,
3-
# so comments can be added in a "comment" key.
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"
Lines changed: 69 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,101 @@
1-
use two_bucket::{solve, Bucket, BucketStats};
1+
use two_bucket::{Bucket, BucketStats};
22

33
#[test]
4-
fn case_1() {
5-
assert_eq!(
6-
solve(3, 5, 1, &Bucket::One),
7-
Some(BucketStats {
8-
moves: 4,
9-
goal_bucket: Bucket::One,
10-
other_bucket: 5,
11-
})
12-
);
4+
fn measure_using_bucket_one_of_size_3_and_bucket_two_of_size_5_start_with_bucket_one() {
5+
let output = two_bucket::solve(3, 5, 1, &Bucket::One);
6+
let expected = Some(BucketStats {
7+
moves: 4,
8+
goal_bucket: Bucket::One,
9+
other_bucket: 5,
10+
});
11+
assert_eq!(output, expected);
1312
}
1413

1514
#[test]
1615
#[ignore]
17-
fn case_2() {
18-
assert_eq!(
19-
solve(3, 5, 1, &Bucket::Two),
20-
Some(BucketStats {
21-
moves: 8,
22-
goal_bucket: Bucket::Two,
23-
other_bucket: 3,
24-
})
25-
);
16+
fn measure_using_bucket_one_of_size_3_and_bucket_two_of_size_5_start_with_bucket_two() {
17+
let output = two_bucket::solve(3, 5, 1, &Bucket::Two);
18+
let expected = Some(BucketStats {
19+
moves: 8,
20+
goal_bucket: Bucket::Two,
21+
other_bucket: 3,
22+
});
23+
assert_eq!(output, expected);
2624
}
2725

2826
#[test]
2927
#[ignore]
30-
fn case_3() {
31-
assert_eq!(
32-
solve(7, 11, 2, &Bucket::One),
33-
Some(BucketStats {
34-
moves: 14,
35-
goal_bucket: Bucket::One,
36-
other_bucket: 11,
37-
})
38-
);
28+
fn measure_using_bucket_one_of_size_7_and_bucket_two_of_size_11_start_with_bucket_one() {
29+
let output = two_bucket::solve(7, 11, 2, &Bucket::One);
30+
let expected = Some(BucketStats {
31+
moves: 14,
32+
goal_bucket: Bucket::One,
33+
other_bucket: 11,
34+
});
35+
assert_eq!(output, expected);
3936
}
4037

4138
#[test]
4239
#[ignore]
43-
fn case_4() {
44-
assert_eq!(
45-
solve(7, 11, 2, &Bucket::Two),
46-
Some(BucketStats {
47-
moves: 18,
48-
goal_bucket: Bucket::Two,
49-
other_bucket: 7,
50-
})
51-
);
40+
fn measure_using_bucket_one_of_size_7_and_bucket_two_of_size_11_start_with_bucket_two() {
41+
let output = two_bucket::solve(7, 11, 2, &Bucket::Two);
42+
let expected = Some(BucketStats {
43+
moves: 18,
44+
goal_bucket: Bucket::Two,
45+
other_bucket: 7,
46+
});
47+
assert_eq!(output, expected);
5248
}
5349

5450
#[test]
5551
#[ignore]
56-
fn goal_equal_to_start_bucket() {
57-
assert_eq!(
58-
solve(1, 3, 3, &Bucket::Two),
59-
Some(BucketStats {
60-
moves: 1,
61-
goal_bucket: Bucket::Two,
62-
other_bucket: 0,
63-
})
64-
);
52+
fn measure_one_step_using_bucket_one_of_size_1_and_bucket_two_of_size_3_start_with_bucket_two() {
53+
let output = two_bucket::solve(1, 3, 3, &Bucket::Two);
54+
let expected = Some(BucketStats {
55+
moves: 1,
56+
goal_bucket: Bucket::Two,
57+
other_bucket: 0,
58+
});
59+
assert_eq!(output, expected);
6560
}
6661

6762
#[test]
6863
#[ignore]
69-
fn goal_equal_to_other_bucket() {
70-
assert_eq!(
71-
solve(2, 3, 3, &Bucket::One),
72-
Some(BucketStats {
73-
moves: 2,
74-
goal_bucket: Bucket::Two,
75-
other_bucket: 2,
76-
})
77-
);
64+
fn measure_using_bucket_one_of_size_2_and_bucket_two_of_size_3_start_with_bucket_one_and_end_with_bucket_two(
65+
) {
66+
let output = two_bucket::solve(2, 3, 3, &Bucket::One);
67+
let expected = Some(BucketStats {
68+
moves: 2,
69+
goal_bucket: Bucket::Two,
70+
other_bucket: 2,
71+
});
72+
assert_eq!(output, expected);
7873
}
7974

8075
#[test]
8176
#[ignore]
8277
fn not_possible_to_reach_the_goal() {
83-
assert_eq!(solve(6, 15, 5, &Bucket::One), None);
78+
let output = two_bucket::solve(6, 15, 5, &Bucket::One);
79+
let expected = None;
80+
assert_eq!(output, expected);
8481
}
8582

8683
#[test]
8784
#[ignore]
88-
fn with_same_buckets_but_different_goal_then_it_is_possible() {
89-
assert_eq!(
90-
solve(6, 15, 9, &Bucket::One),
91-
Some(BucketStats {
92-
moves: 10,
93-
goal_bucket: Bucket::Two,
94-
other_bucket: 0,
95-
})
96-
);
85+
fn with_the_same_buckets_but_a_different_goal_then_it_is_possible() {
86+
let output = two_bucket::solve(6, 15, 9, &Bucket::One);
87+
let expected = Some(BucketStats {
88+
moves: 10,
89+
goal_bucket: Bucket::Two,
90+
other_bucket: 0,
91+
});
92+
assert_eq!(output, expected);
93+
}
94+
95+
#[test]
96+
#[ignore]
97+
fn goal_larger_than_both_buckets_is_impossible() {
98+
let output = two_bucket::solve(5, 7, 8, &Bucket::One);
99+
let expected = None;
100+
assert_eq!(output, expected);
97101
}

0 commit comments

Comments
 (0)