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
15 changes: 9 additions & 6 deletions exercises/two-bucket/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct BucketStats {
}

/// Solve the bucket problem
pub fn solve(capacity_1: u8, capacity_2: u8, goal: u8, start_bucket: &Bucket) -> BucketStats {
pub fn solve(capacity_1: u8, capacity_2: u8, goal: u8, start_bucket: &Bucket) -> Option<BucketStats> {
let state = match *start_bucket {
Bucket::One => (capacity_1, 0),
Bucket::Two => (0, capacity_2),
Expand All @@ -48,25 +48,25 @@ pub fn solve(capacity_1: u8, capacity_2: u8, goal: u8, start_bucket: &Bucket) ->
visited.insert((capacity_1, 0));
visited.insert((0, capacity_2));

loop {
while !next_search.is_empty() {
let mut current_search = next_search;
next_search = VecDeque::new();

for state in current_search.drain(0..) {
let (bucket_1, bucket_2) = state;

if bucket_1 == goal {
return BucketStats {
return Some(BucketStats {
moves,
goal_bucket: Bucket::One,
other_bucket: bucket_2,
};
});
} else if bucket_2 == goal {
return BucketStats {
return Some(BucketStats {
moves,
goal_bucket: Bucket::Two,
other_bucket: bucket_1,
};
});
}

// Empty the first bucket
Expand Down Expand Up @@ -122,4 +122,7 @@ pub fn solve(capacity_1: u8, capacity_2: u8, goal: u8, start_bucket: &Bucket) ->

moves += 1;
}

// We ran out of states to search but still didn't reach the goal.
None
}
4 changes: 2 additions & 2 deletions exercises/two-bucket/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ pub struct BucketStats {
}

/// Solve the bucket problem
pub fn solve(capacity_1: u8, capacity_2: u8, goal: u8, start_bucket: &Bucket) -> BucketStats {
pub fn solve(capacity_1: u8, capacity_2: u8, goal: u8, start_bucket: &Bucket) -> Option<BucketStats> {
unimplemented!(
"Given one bucket of capacity {}, another of capacity {}, starting with {:?}, find pours to reach {}",
"Given one bucket of capacity {}, another of capacity {}, starting with {:?}, find pours to reach {}, or None if impossible",
capacity_1,
capacity_2,
start_bucket,
Expand Down
46 changes: 34 additions & 12 deletions exercises/two-bucket/tests/two-bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use two_bucket::{solve, Bucket, BucketStats};
fn test_case_1() {
assert_eq!(
solve(3, 5, 1, &Bucket::One),
BucketStats {
Some(BucketStats {
moves: 4,
goal_bucket: Bucket::One,
other_bucket: 5,
}
})
);
}

Expand All @@ -17,11 +17,11 @@ fn test_case_1() {
fn test_case_2() {
assert_eq!(
solve(3, 5, 1, &Bucket::Two),
BucketStats {
Some(BucketStats {
moves: 8,
goal_bucket: Bucket::Two,
other_bucket: 3,
}
})
);
}

Expand All @@ -30,11 +30,11 @@ fn test_case_2() {
fn test_case_3() {
assert_eq!(
solve(7, 11, 2, &Bucket::One),
BucketStats {
Some(BucketStats {
moves: 14,
goal_bucket: Bucket::One,
other_bucket: 11,
}
})
);
}

Expand All @@ -43,11 +43,11 @@ fn test_case_3() {
fn test_case_4() {
assert_eq!(
solve(7, 11, 2, &Bucket::Two),
BucketStats {
Some(BucketStats {
moves: 18,
goal_bucket: Bucket::Two,
other_bucket: 7,
}
})
);
}

Expand All @@ -56,11 +56,11 @@ fn test_case_4() {
fn goal_equal_to_start_bucket() {
assert_eq!(
solve(1, 3, 3, &Bucket::Two),
BucketStats {
Some(BucketStats {
moves: 1,
goal_bucket: Bucket::Two,
other_bucket: 0,
}
})
);
}

Expand All @@ -69,10 +69,32 @@ fn goal_equal_to_start_bucket() {
fn goal_equal_to_other_bucket() {
assert_eq!(
solve(2, 3, 3, &Bucket::One),
BucketStats {
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,
})
);
}