Avatar of w1zeman1p

w1zeman1p's solution

to Two Bucket in the Ruby Track

Published at Feb 19 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.


For installation and learning resources, refer to the Ruby resources page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

gem install minitest

If you would like color output, you can require 'minitest/pride' in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

ruby two_bucket_test.rb

To include color from the command line:

ruby -r minitest/pride two_bucket_test.rb

Source

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

Submitting Incomplete Solutions

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

two_bucket_test.rb

require 'minitest/autorun'
require_relative 'two_bucket'

# Common test data version: 1.4.0 edbc86b
class TwoBucketTest < Minitest::Test
  def test_bucket_one_size_3_bucket_two_size_5_goal_1_start_with_bucket_one
    # skip
    subject = TwoBucket.new(3, 5, 1, 'one')
    assert_equal 4, subject.moves
    assert_equal 'one', subject.goal_bucket
    assert_equal 5, subject.other_bucket
  end

  def test_bucket_one_size_3_bucket_two_size_5_goal_1_start_with_bucket_two
    skip
    subject = TwoBucket.new(3, 5, 1, 'two')
    assert_equal 8, subject.moves
    assert_equal 'two', subject.goal_bucket
    assert_equal 3, subject.other_bucket
  end

  def test_bucket_one_size_7_bucket_two_size_11_goal_2_start_with_bucket_one
    skip
    subject = TwoBucket.new(7, 11, 2, 'one')
    assert_equal 14, subject.moves
    assert_equal 'one', subject.goal_bucket
    assert_equal 11, subject.other_bucket
  end

  def test_bucket_one_size_7_bucket_two_size_11_goal_2_start_with_bucket_two
    skip
    subject = TwoBucket.new(7, 11, 2, 'two')
    assert_equal 18, subject.moves
    assert_equal 'two', subject.goal_bucket
    assert_equal 7, subject.other_bucket
  end

  def test_bucket_one_size_1_bucket_two_size_3_goal_3_start_with_bucket_two
    skip
    subject = TwoBucket.new(1, 3, 3, 'two')
    assert_equal 1, subject.moves
    assert_equal 'two', subject.goal_bucket
    assert_equal 0, subject.other_bucket
  end

  def test_bucket_one_size_2_bucket_two_size_3_goal_3_start_with_bucket_one
    skip
    subject = TwoBucket.new(2, 3, 3, 'one')
    assert_equal 2, subject.moves
    assert_equal 'two', subject.goal_bucket
    assert_equal 2, subject.other_bucket
  end
end
class Bucket
  attr_reader :size
  attr_accessor :holding

  def initialize(size, holding)
    @size = size
    @holding = holding
  end

  def fill!
    @holding = @size
  end

  def empty!
    @holding = 0
  end

  def full?
    @holding == @size
  end

  def empty?
    @holding == 0
  end

  def available
    @size - @holding
  end
end

class TwoBucket
  attr_reader :bucket_one
  attr_reader :bucket_two
  attr_reader :liters
  attr_reader :parent

  def initialize(one_size, two_size, liters, first, one_holding: 0, two_holding: 0, parent: nil)
    @parent = parent
    @bucket_one = Bucket.new(one_size, one_holding)
    @bucket_two = Bucket.new(two_size, two_holding)
    @liters = liters

    if first == 'one'
      bucket_one.fill!
    elsif first == 'two'
      bucket_two.fill!
    end
  end

  def to_s
    holding_pair.to_s
  end

  def moves
    if !@moves
      final_state
    end
    @moves = final_state.parent_count
  end

  def parent_count
    count = 0
    state = self
    until state.nil?
      state = state.parent
      count += 1
    end
    count
  end

  def final_state
    @final_state ||= begin
      seen = {}
      ideal_states = []
      children = [self]
      while !children.empty?
        child = children.pop
        if child.holding_pair.include?(liters)
          ideal_states << child
          next
        end

        next if seen.has_key?(child.holding_pair)
        seen[child.holding_pair] = true

        operations = [
          child.clone.pour_one_to_two,
          child.clone.pour_two_to_one,
          child.clone.fill_two,
          child.clone.empty_one,
        ]
        if !child.bucket_two.empty?
          operations << child.clone.fill_one
        end
        if !child.bucket_one.full?
          operations << child.clone.empty_two
        end

        operations.each do |sub|
          next if sub.holding_pair == child.holding_pair
          next if sub.holding_pair == [0, 0]
          children << sub
        end
      end
      ideal_states.min_by {|x| x.parent_count}
    end
  end

  def holding_pair
    [bucket_one.holding, bucket_two.holding]
  end

  def clone
    TwoBucket.new(
      bucket_one.size,
      bucket_two.size, liters,
      nil,
      one_holding: bucket_one.holding,
      two_holding: bucket_two.holding,
      parent: self
    )
  end

  def goal_bucket
    final_state.bucket_one.holding == liters ? 'one' : 'two'
  end

  def other_bucket
    if goal_bucket == 'one'
      final_state.bucket_two.holding
    else
      final_state.bucket_one.holding
    end
  end

  def empty_one
    bucket_one.empty!
    self
  end

  def empty_two
    bucket_two.empty!
    self
  end

  def fill_one
    bucket_one.fill!
    self
  end

  def fill_two
    bucket_two.fill!
    self
  end

  def pour_one_to_two
    if bucket_one.holding < bucket_two.available
      transfer = bucket_one.holding
    else
      transfer = bucket_two.available
    end

    bucket_two.holding += transfer
    bucket_one.holding -= transfer
    self
  end

  def pour_two_to_one
    if bucket_two.holding < bucket_one.available
      transfer = bucket_two.holding
    else
      transfer = bucket_one.available
    end

    bucket_one.holding += transfer
    bucket_two.holding -= transfer
    self
  end
end

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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?