Published at Oct 05 2019
·
0 comments

Instructions

Test suite

Solution

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

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!

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.

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.

To conclude, the only valid moves are:

- pouring from one bucket to another
- emptying one bucket and doing nothing to the other
- filling one bucket and doing nothing to the other

Written with <3 at Fullstack Academy by Lindsay Levine.

Refer to the exercism help page for Rust installation and learning resources.

Execute the tests with:

```
$ cargo test
```

All but the first test have been ignored. After you get the first test to
pass, open the tests source file which is located in the `tests`

directory
and remove the `#[ignore]`

flag from the next test and get the tests to pass
again. Each separate test is a function with `#[test]`

flag above it.
Continue, until you pass every test.

If you wish to run all ignored tests without editing the tests source file, use:

```
$ cargo test -- --ignored
```

To run a specific test, for example `some_test`

, you can use:

```
$ cargo test some_test
```

If the specific test is ignored use:

```
$ cargo test some_test -- --ignored
```

To learn more about Rust tests refer to the online test documentation

Make sure to read the Modules chapter if you haven't already, it will help you with organizing your files.

After you have solved the exercise, please consider using the additional utilities, described in the installation guide, to further refine your final solution.

To format your solution, inside the solution directory use

```
cargo fmt
```

To see, if your solution contains some common ineffective use cases, inside the solution directory use

```
cargo clippy --all-targets
```

Generally you should submit all files in which you implemented your solution (`src/lib.rs`

in most cases). If you are using any external crates, please consider submitting the `Cargo.toml`

file. This will make the review process faster and clearer.

The exercism/rust repository on GitHub is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!

If you want to know more about Exercism, take a look at the contribution guide.

Water Pouring Problem http://demonstrations.wolfram.com/WaterPouringProblem/

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
use two_bucket::{solve, Bucket, BucketStats};
#[test]
fn test_case_1() {
assert_eq!(
solve(3, 5, 1, &Bucket::One),
Some(BucketStats {
moves: 4,
goal_bucket: Bucket::One,
other_bucket: 5,
})
);
}
#[test]
#[ignore]
fn test_case_2() {
assert_eq!(
solve(3, 5, 1, &Bucket::Two),
Some(BucketStats {
moves: 8,
goal_bucket: Bucket::Two,
other_bucket: 3,
})
);
}
#[test]
#[ignore]
fn test_case_3() {
assert_eq!(
solve(7, 11, 2, &Bucket::One),
Some(BucketStats {
moves: 14,
goal_bucket: Bucket::One,
other_bucket: 11,
})
);
}
#[test]
#[ignore]
fn test_case_4() {
assert_eq!(
solve(7, 11, 2, &Bucket::Two),
Some(BucketStats {
moves: 18,
goal_bucket: Bucket::Two,
other_bucket: 7,
})
);
}
#[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,
})
);
}
#[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,
})
);
}
#[test]
#[ignore]
fn not_possible_to_reach_the_goal() {
assert_eq!(
solve(6, 15, 5, &Bucket::One),
None
);
}
#[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,
})
);
}
```

```
use std::collections::HashSet;
#[derive(PartialEq, Eq, Debug)]
pub enum Bucket {
One,
Two,
}
#[derive(PartialEq, Eq, Debug)]
pub struct BucketStats {
pub moves: u8,
pub goal_bucket: Bucket,
pub other_bucket: u8,
}
#[derive(Debug)]
struct BucketInstance {
capacity: u8,
holds: u8,
}
impl BucketInstance {
fn new(cap: u8) -> Self {
BucketInstance {
capacity: cap,
holds: 0,
}
}
fn fill(&self) -> Self {
BucketInstance {
capacity: self.capacity,
holds: self.capacity,
}
}
fn throw(&self) -> Self {
BucketInstance {
capacity: self.capacity,
holds: 0,
}
}
fn pour_in(&self, amount: u8) -> Self {
let mut hold = self.holds + amount;
if hold > self.capacity {
hold = self.capacity;
}
BucketInstance {
capacity: self.capacity,
holds: hold,
}
}
fn pour_out(&self, amount: u8) -> Self {
let hold;
if amount >= self.holds {
hold = 0;
} else {
hold = self.holds - amount;
}
BucketInstance {
capacity: self.capacity,
holds: hold,
}
}
fn pour_amount(&self, other: &BucketInstance) -> u8 {
let cap = self.capacity - self.holds;
if other.holds >= cap {
cap
} else {
other.holds
}
}
fn no_move(&self) -> Self {
BucketInstance {
capacity: self.capacity,
holds: self.holds,
}
}
}
#[derive(Debug)]
struct State {
moves: u8,
bucket_one: BucketInstance,
bucket_two: BucketInstance,
last_move: Bucket,
}
enum Moves {
Fill,
Throw,
Pour,
}
impl State {
fn new(cap1: u8, cap2: u8, start: Bucket) -> Self {
let mut bucket_one = BucketInstance::new(cap1);
let mut bucket_two = BucketInstance::new(cap2);
match start {
Bucket::One => bucket_one = bucket_one.fill(),
Bucket::Two => bucket_two = bucket_two.fill(),
}
State {
last_move: start,
moves: 1,
bucket_one,
bucket_two,
}
}
fn fill(&self, bucket: Bucket) -> Self {
match bucket {
Bucket::One => State {
last_move: bucket,
moves: self.moves + 1,
bucket_one: self.bucket_one.fill(),
bucket_two: self.bucket_two.no_move(),
},
Bucket::Two => State {
last_move: bucket,
moves: self.moves + 1,
bucket_one: self.bucket_one.no_move(),
bucket_two: self.bucket_two.fill(),
},
}
}
fn throw(&self, bucket: Bucket) -> Self {
match bucket {
Bucket::One => State {
last_move: bucket,
moves: self.moves + 1,
bucket_one: self.bucket_one.throw(),
bucket_two: self.bucket_two.no_move(),
},
Bucket::Two => State {
last_move: bucket,
moves: self.moves + 1,
bucket_one: self.bucket_one.no_move(),
bucket_two: self.bucket_two.throw(),
},
}
}
fn pour(&self, bucket: Bucket) -> Self {
match bucket {
Bucket::One => {
let amount = self.bucket_two.pour_amount(&self.bucket_one);
State {
last_move: bucket,
moves: self.moves + 1,
bucket_one: self.bucket_one.pour_out(amount),
bucket_two: self.bucket_two.pour_in(amount),
}
}
Bucket::Two => {
let amount = self.bucket_one.pour_amount(&self.bucket_two);
State {
last_move: bucket,
moves: self.moves + 1,
bucket_one: self.bucket_one.pour_in(amount),
bucket_two: self.bucket_two.pour_out(amount),
}
}
}
}
fn get_state(&self) -> (u8, u8) {
(self.bucket_one.holds, self.bucket_two.holds)
}
fn find(self, goal: u8, hist: &mut HashSet<(u8, u8)>) -> Option<State> {
let stat = self.get_state();
if stat.0 == goal || stat.1 == goal {
println!("{:?}", self);
return Some(self);
}
let mut next: Vec<State> = Vec::new();
for b in [Bucket::One, Bucket::Two].iter() {
for m in [Moves::Fill, Moves::Pour, Moves::Throw].iter() {
let n = match b {
Bucket::One => match m {
Moves::Fill => {
if self.bucket_one.holds < self.bucket_one.capacity {
Some(self.fill(Bucket::One))
} else {
None
}
}
Moves::Pour => {
if self.bucket_two.holds < self.bucket_two.capacity {
Some(self.pour(Bucket::One))
} else {
None
}
}
Moves::Throw => {
if self.bucket_one.holds > 0 {
Some(self.throw(Bucket::One))
} else {
None
}
}
},
Bucket::Two => match m {
Moves::Fill => {
if self.bucket_two.holds < self.bucket_two.capacity {
Some(self.fill(Bucket::Two))
} else {
None
}
}
Moves::Pour => {
if self.bucket_one.holds < self.bucket_one.capacity {
Some(self.pour(Bucket::Two))
} else {
None
}
}
Moves::Throw => {
if self.bucket_one.holds > 0 {
Some(self.throw(Bucket::Two))
} else {
None
}
}
},
};
if let Some(n) = n {
let stat = n.get_state();
if !hist.contains(&stat) {
hist.insert(stat);
next.push(n);
}
}
}
}
let mut solved: Vec<State> = Vec::new();
for n in next {
if let Some(x) = n.find(goal, hist) {
solved.push(x)
}
}
if solved.len() == 0 {
return None;
} else {
solved.sort_by(|a, b| b.moves.cmp(&a.moves));
return solved.pop();
}
}
}
pub fn solve(
capacity_1: u8,
capacity_2: u8,
goal: u8,
start_bucket: &Bucket,
) -> Option<BucketStats> {
let start = if let Bucket::One = *start_bucket {
Bucket::One
} else {
Bucket::Two
};
let mut hist: HashSet<(u8, u8)> = HashSet::new();
let state = State::new(capacity_1, capacity_2, start);
println!("{:?}", state);
if let Some(x) = state.find(goal, &mut hist) {
let margin;
if x.bucket_one.holds == goal {
margin = x.bucket_two.holds;
} else {
margin = x.bucket_one.holds;
}
return Some(BucketStats {
moves: x.moves,
goal_bucket: x.last_move,
other_bucket: margin,
});
}
None
}
```

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,089 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments