Skip to content

Commit

Permalink
Merge pull request #10 from kvsm/Two-bucket-regenerate_tests
Browse files Browse the repository at this point in the history
Fix two-bucket solution

Added short-cut for case where non-starting bucket size is equal to goal
  • Loading branch information
Insti authored Oct 19, 2017
2 parents a61feec + 02a0d01 commit 6de930f
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions exercises/two-bucket/.meta/solutions/two_bucket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6de930f

Please sign in to comment.