Skip to content

Commit

Permalink
Add exercise two-bucket. (exercism#785)
Browse files Browse the repository at this point in the history
  • Loading branch information
siebenschlaefer authored Jan 25, 2024
1 parent 9be83e4 commit 13cf17d
Show file tree
Hide file tree
Showing 12 changed files with 18,373 additions and 0 deletions.
9 changes: 9 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,15 @@
"prerequisites": [],
"difficulty": 5,
"topics": []
},
{
"slug": "two-bucket",
"name": "Two Bucket",
"uuid": "83c92b7f-fb0e-43d8-ab7d-d514408b8575",
"practices": [],
"prerequisites": [],
"difficulty": 7,
"topics": []
}
],
"foregone": [
Expand Down
46 changes: 46 additions & 0 deletions exercises/practice/two-bucket/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Instructions

Given two buckets of different size and which bucket to fill first, determine how many actions are required to measure an exact number of liters by strategically transferring fluid between the buckets.

There are some rules that your solution must follow:

- You can only do one action at a time.
- There are only 3 possible actions:
1. Pouring one bucket into the other bucket until either:
a) the first bucket is empty
b) the second bucket is full
2. Emptying a bucket and doing nothing to the other.
3. Filling a bucket and doing nothing to the other.
- After an action, you may not arrive at a state where the starting bucket is empty and the other bucket is full.

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 actions it should take to reach the desired number of liters, including the first fill of the starting bucket
- which bucket should end up with the desired number of liters - either bucket one or bucket two
- how many liters are left in the other bucket

Note: any time a change is made to either or both buckets counts as one (1) action.

Example:
Bucket one can hold up to 7 liters, and bucket two can hold up to 11 liters.
Let's say at a given step, bucket one 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 action.
Instead, if you had poured from bucket one into bucket two until bucket two was full, resulting in 4 liters in bucket one and 11 liters in bucket two (4,11), that would also only count as one action.

Another Example:
Bucket one can hold 3 liters, and bucket two can hold up to 5 liters.
You are told you must start with bucket one.
So your first action is to fill bucket one.
You choose to empty bucket one for your second action.
For your third action, you may not fill bucket two, because this violates the third rule -- you may not end up in a state after any action where the starting bucket is empty and the other bucket is full.

Written with <3 at [Fullstack Academy][fullstack] by Lindsay Levine.

[fullstack]: https://www.fullstackacademy.com/
21 changes: 21 additions & 0 deletions exercises/practice/two-bucket/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"authors": [
"siebenschlaefer"
],
"files": {
"solution": [
"two_bucket.cpp",
"two_bucket.h"
],
"test": [
"two_bucket_test.cpp"
],
"example": [
".meta/example.cpp",
".meta/example.h"
]
},
"blurb": "Given two buckets of different size, demonstrate how to measure an exact number of liters.",
"source": "Water Pouring Problem",
"source_url": "https://demonstrations.wolfram.com/WaterPouringProblem/"
}
57 changes: 57 additions & 0 deletions exercises/practice/two-bucket/.meta/example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "two_bucket.h"

#include <stdexcept>

two_bucket::measure_result two_bucket::measure(
int bucket1_capacity, int bucket2_capacity,
int target_volume, bucket_id start_bucket)
{
if (start_bucket == bucket_id::two)
{
auto result = measure(
bucket2_capacity, bucket1_capacity,
target_volume, bucket_id::one);
result.goal_bucket =
(result.goal_bucket == bucket_id::one)
? bucket_id::two
: bucket_id::one;
return result;
}

if (target_volume == 0)
return {0, bucket_id::one, 0};
if (target_volume == bucket1_capacity)
return {1, bucket_id::one, 0};
if (target_volume == bucket2_capacity)
return {2, bucket_id::two, bucket1_capacity};

int num_steps = 0;
int volume1 = 0;
int volume2 = 0;
while (volume1 != 0 || volume2 != bucket2_capacity)
{
num_steps += 2;
if (volume1 == 0)
volume1 = bucket1_capacity; // fill(one)
else
volume2 = 0; // empty(two)

// pour(one, two)
if (volume1 + volume2 <= bucket2_capacity)
{
volume2 += volume1;
volume1 = 0;
if (volume2 == target_volume)
return {num_steps, bucket_id::two, 0};
}
else // if volume1 + volume2 > bucket2_max
{
volume1 -= bucket2_capacity - volume2;
volume2 = bucket2_capacity;
if (volume1 == target_volume)
return {num_steps, bucket_id::one, bucket2_capacity};
}
}
throw std::invalid_argument("impossible");
}

22 changes: 22 additions & 0 deletions exercises/practice/two-bucket/.meta/example.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef TWO_BUCKET_H
#define TWO_BUCKET_H

namespace two_bucket
{

enum class bucket_id { one, two };

struct measure_result
{
int num_moves;
bucket_id goal_bucket;
int other_bucket_volume;
};

measure_result measure(
int bucket1_capacity, int bucket2_capacity,
int target_volume, bucket_id start_bucket);

} // namespace two_bucket

#endif // TWO_BUCKET_H
37 changes: 37 additions & 0 deletions exercises/practice/two-bucket/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[a6f2b4ba-065f-4dca-b6f0-e3eee51cb661]
description = "Measure using bucket one of size 3 and bucket two of size 5 - start with bucket one"

[6c4ea451-9678-4926-b9b3-68364e066d40]
description = "Measure using bucket one of size 3 and bucket two of size 5 - start with bucket two"

[3389f45e-6a56-46d5-9607-75aa930502ff]
description = "Measure using bucket one of size 7 and bucket two of size 11 - start with bucket one"

[fe0ff9a0-3ea5-4bf7-b17d-6d4243961aa1]
description = "Measure using bucket one of size 7 and bucket two of size 11 - start with bucket two"

[0ee1f57e-da84-44f7-ac91-38b878691602]
description = "Measure one step using bucket one of size 1 and bucket two of size 3 - start with bucket two"

[eb329c63-5540-4735-b30b-97f7f4df0f84]
description = "Measure using bucket one of size 2 and bucket two of size 3 - start with bucket one and end with bucket two"

[449be72d-b10a-4f4b-a959-ca741e333b72]
description = "Not possible to reach the goal"

[aac38b7a-77f4-4d62-9b91-8846d533b054]
description = "With the same buckets but a different goal, then it is possible"

[74633132-0ccf-49de-8450-af4ab2e3b299]
description = "Goal larger than both buckets is impossible"
64 changes: 64 additions & 0 deletions exercises/practice/two-bucket/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Get the exercise name from the current directory
get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME)

# Basic CMake project
cmake_minimum_required(VERSION 3.5.1)

# Name the project after the exercise
project(${exercise} CXX)

# Get a source filename from the exercise name by replacing -'s with _'s
string(REPLACE "-" "_" file ${exercise})

# Implementation could be only a header
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.cpp)
set(exercise_cpp ${file}.cpp)
else()
set(exercise_cpp "")
endif()

# Use the common Catch library?
if(EXERCISM_COMMON_CATCH)
# For Exercism track development only
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h $<TARGET_OBJECTS:catchlib>)
elseif(EXERCISM_TEST_SUITE)
# The Exercism test suite is being run, the Docker image already
# includes a pre-built version of Catch.
find_package(Catch2 REQUIRED)
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h)
target_link_libraries(${exercise} PRIVATE Catch2::Catch2WithMain)
# When Catch is installed system wide we need to include a different
# header, we need this define to use the correct one.
target_compile_definitions(${exercise} PRIVATE EXERCISM_TEST_SUITE)
else()
# Build executable from sources and headers
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h test/tests-main.cpp)
endif()

set_target_properties(${exercise} PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED OFF
CXX_EXTENSIONS OFF
)

set(CMAKE_BUILD_TYPE Debug)

if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(GNU|Clang)")
set_target_properties(${exercise} PROPERTIES
COMPILE_FLAGS "-Wall -Wextra -Wpedantic -Werror"
)
endif()

# Configure to run all the tests?
if(${EXERCISM_RUN_ALL_TESTS})
target_compile_definitions(${exercise} PRIVATE EXERCISM_RUN_ALL_TESTS)
endif()

# Tell MSVC not to warn us about unchecked iterators in debug builds
if(${MSVC})
set_target_properties(${exercise} PROPERTIES
COMPILE_DEFINITIONS_DEBUG _SCL_SECURE_NO_WARNINGS)
endif()

# Run the tests on every build
add_custom_target(test_${exercise} ALL DEPENDS ${exercise} COMMAND ${exercise})
Loading

0 comments on commit 13cf17d

Please sign in to comment.