diff --git a/.gitignore b/.gitignore index edcc454ae..5af0f3619 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ tmp exercises/*/*/Cargo.lock exercises/*/*/clippy.log .vscode +.prob-spec diff --git a/bin/symlink_problem_specifications.sh b/bin/symlink_problem_specifications.sh new file mode 100755 index 000000000..bf86ebaa9 --- /dev/null +++ b/bin/symlink_problem_specifications.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +set -eo pipefail + +cd "$(git rev-parse --show-toplevel)" + +for exercise in exercises/practice/*; do + name="$(basename "$exercise")" + if [ -d "problem-specifications/exercises/$name" ]; then + [ -e "$exercise/.prob-spec" ] && rm "$exercise/.prob-spec" + ln -s "../../../problem-specifications/exercises/$name" "$exercise/.prob-spec" + fi +done diff --git a/docs/CONTRIBUTING.md b/docs/CONTRIBUTING.md index eef87865c..7d2e57e05 100644 --- a/docs/CONTRIBUTING.md +++ b/docs/CONTRIBUTING.md @@ -101,6 +101,11 @@ Find some tips about writing tera templates [in the next section](#tera-template The full documentation for tera templates is [here][tera-docs]. Following are some approaches that have worked for our specific needs. +You will likely want to look at the exercise's `canonical-data.json` +to see what structure your input data has. +You can use `bin/symlink_problem_specifications.sh` to have this data +symlinked into the actual exercise directory. Handy! + The name of the input property is different for each exercise. The default template will be something like this: diff --git a/exercises/practice/two-bucket/.docs/instructions.md b/exercises/practice/two-bucket/.docs/instructions.md index 73eb450ce..7249deb36 100644 --- a/exercises/practice/two-bucket/.docs/instructions.md +++ b/exercises/practice/two-bucket/.docs/instructions.md @@ -1,30 +1,46 @@ # Instructions -Given two buckets of different size, demonstrate how to measure an exact number of liters by strategically transferring liters of fluid between the buckets. +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. -Since this mathematical problem is fairly subject to interpretation / individual approach, the tests have been written specifically to expect one overarching solution. +There are some rules that your solution must follow: -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! +- 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 "moves" it should take to reach the desired number of liters, including the first fill -- which bucket should end up with the desired number of liters (let's say this is bucket A) - either bucket one or bucket two -- how many liters are left in the other bucket (bucket B) -Note: any time a change is made to either or both buckets counts as one (1) move. +- 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 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. +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. -To conclude, the only valid moves are: -- pouring from either bucket to another -- emptying either bucket and doing nothing to the other -- filling either bucket and doing nothing to the other +Written with <3 at [Fullstack Academy][fullstack] by Lindsay Levine. -Written with <3 at [Fullstack Academy](http://www.fullstackacademy.com/) 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 index aa046009b..bcc6a5e36 100644 --- a/exercises/practice/two-bucket/.meta/config.json +++ b/exercises/practice/two-bucket/.meta/config.json @@ -30,5 +30,5 @@ }, "blurb": "Given two buckets of different size, demonstrate how to measure an exact number of liters.", "source": "Water Pouring Problem", - "source_url": "http://demonstrations.wolfram.com/WaterPouringProblem/" + "source_url": "https://demonstrations.wolfram.com/WaterPouringProblem/" } diff --git a/exercises/practice/two-bucket/.meta/test_template.tera b/exercises/practice/two-bucket/.meta/test_template.tera new file mode 100644 index 000000000..b6de2697b --- /dev/null +++ b/exercises/practice/two-bucket/.meta/test_template.tera @@ -0,0 +1,31 @@ +{% macro bucket(label) -%} + {% if label == 'one' -%} + Bucket::One + {%- else -%} + Bucket::Two + {%- endif %} +{%- endmacro bucket -%} + +use two_bucket::{Bucket, BucketStats}; +{% for test in cases %} +#[test] +{% if loop.index != 1 -%} +#[ignore] +{% endif -%} +fn {{ test.description | slugify | replace(from="-", to="_") }}() { + let output = {{ crate_name }}::{{ fn_names[0] }}( + {{ test.input.bucketOne }}, {{ test.input.bucketTwo }}, {{ test.input.goal }}, + &{{ self::bucket(label=test.input.startBucket) }}, + ); + let expected = {% if 'error' in test.expected -%} + None + {%- else -%} + Some(BucketStats { + moves: {{ test.expected.moves }}, + goal_bucket: {{ self::bucket(label=test.expected.goalBucket) }}, + other_bucket: {{ test.expected.otherBucket }}, + }) + {%- endif %}; + assert_eq!(output, expected); +} +{% endfor -%} diff --git a/exercises/practice/two-bucket/.meta/tests.toml b/exercises/practice/two-bucket/.meta/tests.toml index be690e975..d6ff02f53 100644 --- a/exercises/practice/two-bucket/.meta/tests.toml +++ b/exercises/practice/two-bucket/.meta/tests.toml @@ -1,3 +1,37 @@ -# This is an auto-generated file. Regular comments will be removed when this -# file is regenerated. Regenerating will not touch any manually added keys, -# so comments can be added in a "comment" key. +# 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/tests/two-bucket.rs b/exercises/practice/two-bucket/tests/two-bucket.rs index 30346ec0d..a21311a25 100644 --- a/exercises/practice/two-bucket/tests/two-bucket.rs +++ b/exercises/practice/two-bucket/tests/two-bucket.rs @@ -1,97 +1,101 @@ -use two_bucket::{solve, Bucket, BucketStats}; +use two_bucket::{Bucket, BucketStats}; #[test] -fn case_1() { - assert_eq!( - solve(3, 5, 1, &Bucket::One), - Some(BucketStats { - moves: 4, - goal_bucket: Bucket::One, - other_bucket: 5, - }) - ); +fn measure_using_bucket_one_of_size_3_and_bucket_two_of_size_5_start_with_bucket_one() { + let output = two_bucket::solve(3, 5, 1, &Bucket::One); + let expected = Some(BucketStats { + moves: 4, + goal_bucket: Bucket::One, + other_bucket: 5, + }); + assert_eq!(output, expected); } #[test] #[ignore] -fn case_2() { - assert_eq!( - solve(3, 5, 1, &Bucket::Two), - Some(BucketStats { - moves: 8, - goal_bucket: Bucket::Two, - other_bucket: 3, - }) - ); +fn measure_using_bucket_one_of_size_3_and_bucket_two_of_size_5_start_with_bucket_two() { + let output = two_bucket::solve(3, 5, 1, &Bucket::Two); + let expected = Some(BucketStats { + moves: 8, + goal_bucket: Bucket::Two, + other_bucket: 3, + }); + assert_eq!(output, expected); } #[test] #[ignore] -fn case_3() { - assert_eq!( - solve(7, 11, 2, &Bucket::One), - Some(BucketStats { - moves: 14, - goal_bucket: Bucket::One, - other_bucket: 11, - }) - ); +fn measure_using_bucket_one_of_size_7_and_bucket_two_of_size_11_start_with_bucket_one() { + let output = two_bucket::solve(7, 11, 2, &Bucket::One); + let expected = Some(BucketStats { + moves: 14, + goal_bucket: Bucket::One, + other_bucket: 11, + }); + assert_eq!(output, expected); } #[test] #[ignore] -fn case_4() { - assert_eq!( - solve(7, 11, 2, &Bucket::Two), - Some(BucketStats { - moves: 18, - goal_bucket: Bucket::Two, - other_bucket: 7, - }) - ); +fn measure_using_bucket_one_of_size_7_and_bucket_two_of_size_11_start_with_bucket_two() { + let output = two_bucket::solve(7, 11, 2, &Bucket::Two); + let expected = Some(BucketStats { + moves: 18, + goal_bucket: Bucket::Two, + other_bucket: 7, + }); + assert_eq!(output, expected); } #[test] #[ignore] -fn goal_equal_to_start_bucket() { - assert_eq!( - solve(1, 3, 3, &Bucket::Two), - Some(BucketStats { - moves: 1, - goal_bucket: Bucket::Two, - other_bucket: 0, - }) - ); +fn measure_one_step_using_bucket_one_of_size_1_and_bucket_two_of_size_3_start_with_bucket_two() { + let output = two_bucket::solve(1, 3, 3, &Bucket::Two); + let expected = Some(BucketStats { + moves: 1, + goal_bucket: Bucket::Two, + other_bucket: 0, + }); + assert_eq!(output, expected); } #[test] #[ignore] -fn goal_equal_to_other_bucket() { - assert_eq!( - solve(2, 3, 3, &Bucket::One), - Some(BucketStats { - moves: 2, - goal_bucket: Bucket::Two, - other_bucket: 2, - }) - ); +fn measure_using_bucket_one_of_size_2_and_bucket_two_of_size_3_start_with_bucket_one_and_end_with_bucket_two( +) { + let output = two_bucket::solve(2, 3, 3, &Bucket::One); + let expected = Some(BucketStats { + moves: 2, + goal_bucket: Bucket::Two, + other_bucket: 2, + }); + assert_eq!(output, expected); } #[test] #[ignore] fn not_possible_to_reach_the_goal() { - assert_eq!(solve(6, 15, 5, &Bucket::One), None); + let output = two_bucket::solve(6, 15, 5, &Bucket::One); + let expected = None; + assert_eq!(output, expected); } #[test] #[ignore] -fn with_same_buckets_but_different_goal_then_it_is_possible() { - assert_eq!( - solve(6, 15, 9, &Bucket::One), - Some(BucketStats { - moves: 10, - goal_bucket: Bucket::Two, - other_bucket: 0, - }) - ); +fn with_the_same_buckets_but_a_different_goal_then_it_is_possible() { + let output = two_bucket::solve(6, 15, 9, &Bucket::One); + let expected = Some(BucketStats { + moves: 10, + goal_bucket: Bucket::Two, + other_bucket: 0, + }); + assert_eq!(output, expected); +} + +#[test] +#[ignore] +fn goal_larger_than_both_buckets_is_impossible() { + let output = two_bucket::solve(5, 7, 8, &Bucket::One); + let expected = None; + assert_eq!(output, expected); }