From 02a0d017a771921d52e8d991c59a4ceec47497ec Mon Sep 17 00:00:00 2001 From: Kevin Smith Date: Thu, 19 Oct 2017 13:04:57 +0100 Subject: [PATCH] Fix two-bucket solution Added short-cut for case where non-starting bucket size is equal to goal --- .../two-bucket/.meta/solutions/two_bucket.rb | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/exercises/two-bucket/.meta/solutions/two_bucket.rb b/exercises/two-bucket/.meta/solutions/two_bucket.rb index 1345886b81..a18fac4c54 100644 --- a/exercises/two-bucket/.meta/solutions/two_bucket.rb +++ b/exercises/two-bucket/.meta/solutions/two_bucket.rb @@ -15,11 +15,15 @@ def initialize(first_size, second_size, goal, start_bucket) def moves if @start_bucket == 'one' @levels = [@first_size, 0] - solve method(:start_from_first) + strategy = method(:start_from_first) else @levels = [0, @second_size] - solve method(:start_from_second) + strategy = method(:start_from_second) end + if other_bucket_matches_goal + strategy = method(:fill_other_bucket) + end + solve strategy end private @@ -68,6 +72,22 @@ def start_from_second end end + def fill_other_bucket + if first_bucket_empty? + fill_first_bucket + else + fill_second_bucket + end + end + + def other_bucket_matches_goal + if @start_bucket == 'one' + return @second_size == @goal + else + return @first_size == @goal + end + end + def first_bucket_empty? @levels.first == 0 end