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:
Your program should determine:
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:
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:
To include color from the command line:
ruby -r minitest/pride two_bucket_test.rb
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.
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
# frozen_string_literal: true class TwoBucket class Bucket EMPTY = 0 private_constant :EMPTY attr_reader :name, :capacity, :contents def initialize(name, capacity) @name = name @capacity = capacity @contents = EMPTY end def empty self.contents = EMPTY end def empty? contents == EMPTY end def fill self.contents = capacity self end def full? contents == capacity end def pour_fluid_into(other) amount = calculate_fluid_amount(other) other.contents += amount self.contents -= amount end protected attr_writer :contents private def calculate_fluid_amount(other) [other.capacity - other.contents, contents].min end end private_constant :Bucket ONE = "one" private_constant :ONE TWO = "two" private_constant :TWO attr_reader :goal_bucket, :moves, :other_bucket def initialize(capacity1, capacity2, goal, start_bucket) @goal = goal @goal_bucket, @other_bucket = [capacity1, capacity2, start_bucket] .then(&method(:initialize_buckets)) .then(&method(:initialize_moves)) end private attr_reader :goal attr_writer :moves def initialize_buckets((capacity1, capacity2, start_bucket)) bucket_one = Bucket.new(ONE, capacity1) bucket_two = Bucket.new(TWO, capacity2) self.moves = 1 if start_bucket == ONE [bucket_one.fill, bucket_two] else [bucket_two.fill, bucket_one] end end def initialize_moves((start_bucket, other_bucket)) loop do return [start_bucket.name, other_bucket.contents] if start_bucket.contents == goal return [other_bucket.name, start_bucket.contents] if other_bucket.contents == goal transfer_fluid(start_bucket, other_bucket) self.moves += 1 end end def transfer_fluid(start_bucket, other_bucket) if start_bucket.empty? start_bucket.fill elsif other_bucket.capacity == goal other_bucket.fill elsif other_bucket.full? other_bucket.empty else start_bucket.pour_fluid_into(other_bucket) end end end
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.