diff --git a/config.json b/config.json index 57436d4e66..2c9324de9c 100644 --- a/config.json +++ b/config.json @@ -2695,6 +2695,26 @@ "integers" ], "difficulty": 3 + }, + { + "slug": "two-bucket", + "name": "Two Bucket", + "uuid": "07b4679a-3d9f-42bc-ad1e-b9ba272a1c15", + "prerequisites": [ + "atoms", + "tuples", + "integers", + "lists", + "pattern-matching", + "if", + "cond", + "case", + "structs", + "enum", + "recursion" + ], + "practices": [], + "difficulty": 7 } ], "foregone": [ diff --git a/exercises/practice/two-bucket/.docs/instructions.md b/exercises/practice/two-bucket/.docs/instructions.md new file mode 100644 index 0000000000..4656c78ce2 --- /dev/null +++ b/exercises/practice/two-bucket/.docs/instructions.md @@ -0,0 +1,30 @@ +# Description + +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 either bucket to another +- emptying either bucket and doing nothing to the other +- filling either bucket and doing nothing to the other + +Written with <3 at [Fullstack Academy](http://www.fullstackacademy.com/) by Lindsay Levine. diff --git a/exercises/practice/two-bucket/.formatter.exs b/exercises/practice/two-bucket/.formatter.exs new file mode 100644 index 0000000000..d2cda26edd --- /dev/null +++ b/exercises/practice/two-bucket/.formatter.exs @@ -0,0 +1,4 @@ +# Used by "mix format" +[ + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] +] diff --git a/exercises/practice/two-bucket/.meta/config.json b/exercises/practice/two-bucket/.meta/config.json new file mode 100644 index 0000000000..133def479f --- /dev/null +++ b/exercises/practice/two-bucket/.meta/config.json @@ -0,0 +1,21 @@ +{ + "authors": ["jiegillet"], + "contributors": [ + "angelikatyborska", + "neenjaw" + ], + "files": { + "example": [ + ".meta/example.ex" + ], + "solution": [ + "lib/two_bucket.ex" + ], + "test": [ + "test/two_bucket_test.exs" + ] + }, + "blurb": "Given two buckets of different size, demonstrate how to measure an exact number of liters.", + "source": "Water Pouring Problem", + "source_url": "http://demonstrations.wolfram.com/WaterPouringProblem/" +} diff --git a/exercises/practice/two-bucket/.meta/example.ex b/exercises/practice/two-bucket/.meta/example.ex new file mode 100644 index 0000000000..d31b2e4bdb --- /dev/null +++ b/exercises/practice/two-bucket/.meta/example.ex @@ -0,0 +1,60 @@ +defmodule TwoBucket do + defstruct [:bucket_one, :bucket_two, :moves] + @type t :: %TwoBucket{bucket_one: integer, bucket_two: integer, moves: integer} + + @doc """ + Find the quickest way to fill a bucket with some amount of water from two buckets of specific sizes. + """ + @spec measure( + size_one :: integer, + size_two :: integer, + goal :: integer, + start_bucket :: :one | :two + ) :: {:ok, TwoBucket.t()} | {:error, :impossible} + def measure(size_one, size_two, goal, start_bucket) do + other_filled = if start_bucket == :one, do: {0, size_two}, else: {size_one, 0} + forbidden_states = MapSet.new([other_filled]) + + # Partially apply function to avoid passing many arguments + next_moves = &next_moves(&1, size_one, size_two) + + pour([{{0, 0}, 0}], forbidden_states, next_moves, goal) + end + + def pour([], _, _, _), do: {:error, :impossible} + + def pour([{{a, b}, moves} | _], _, _, goal) when a == goal or b == goal, + do: {:ok, %TwoBucket{bucket_one: a, bucket_two: b, moves: moves}} + + def pour([{state, moves} | states], forbidden_states, next_moves, goal) do + next = + next_moves.(state) + |> Enum.reject(&MapSet.member?(forbidden_states, &1)) + |> Enum.map(&{&1, moves + 1}) + + pour(states ++ next, MapSet.put(forbidden_states, state), next_moves, goal) + end + + def next_moves({fill_one, fill_two}, size_one, size_two) do + [ + # Empty a bucket + {0, fill_two}, + {fill_one, 0}, + # Fill a bucket + {fill_one, size_two}, + {size_one, fill_two}, + # Pour one into the other + if fill_one < size_two - fill_two do + {0, fill_one + fill_two} + else + {fill_one - (size_two - fill_two), size_two} + end, + if fill_two < size_one - fill_one do + {fill_one + fill_two, 0} + else + {size_one, fill_two - (size_one - fill_one)} + end + ] + |> Enum.uniq() + end +end diff --git a/exercises/practice/two-bucket/.meta/tests.toml b/exercises/practice/two-bucket/.meta/tests.toml new file mode 100644 index 0000000000..eb3561e8e4 --- /dev/null +++ b/exercises/practice/two-bucket/.meta/tests.toml @@ -0,0 +1,36 @@ +# 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" diff --git a/exercises/practice/two-bucket/lib/two_bucket.ex b/exercises/practice/two-bucket/lib/two_bucket.ex new file mode 100644 index 0000000000..38a6862449 --- /dev/null +++ b/exercises/practice/two-bucket/lib/two_bucket.ex @@ -0,0 +1,16 @@ +defmodule TwoBucket do + defstruct [:bucket_one, :bucket_two, :moves] + @type t :: %TwoBucket{bucket_one: integer, bucket_two: integer, moves: integer} + + @doc """ + Find the quickest way to fill a bucket with some amount of water from two buckets of specific sizes. + """ + @spec measure( + size_one :: integer, + size_two :: integer, + goal :: integer, + start_bucket :: :one | :two + ) :: {:ok, TwoBucket.t()} | {:error, :impossible} + def measure(size_one, size_two, goal, start_bucket) do + end +end diff --git a/exercises/practice/two-bucket/mix.exs b/exercises/practice/two-bucket/mix.exs new file mode 100644 index 0000000000..2edcd8839b --- /dev/null +++ b/exercises/practice/two-bucket/mix.exs @@ -0,0 +1,28 @@ +defmodule TwoBucket.MixProject do + use Mix.Project + + def project do + [ + app: :two_bucket, + version: "0.1.0", + # elixir: "~> 1.8", + start_permanent: Mix.env() == :prod, + deps: deps() + ] + end + + # Run "mix help compile.app" to learn about applications. + def application do + [ + extra_applications: [:logger] + ] + end + + # Run "mix help deps" to learn about dependencies. + defp deps do + [ + # {:dep_from_hexpm, "~> 0.3.0"}, + # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} + ] + end +end diff --git a/exercises/practice/two-bucket/test/test_helper.exs b/exercises/practice/two-bucket/test/test_helper.exs new file mode 100644 index 0000000000..35fc5bff82 --- /dev/null +++ b/exercises/practice/two-bucket/test/test_helper.exs @@ -0,0 +1,2 @@ +ExUnit.start() +ExUnit.configure(exclude: :pending, trace: true) diff --git a/exercises/practice/two-bucket/test/two_bucket_test.exs b/exercises/practice/two-bucket/test/two_bucket_test.exs new file mode 100644 index 0000000000..a48ddbe8a1 --- /dev/null +++ b/exercises/practice/two-bucket/test/two_bucket_test.exs @@ -0,0 +1,111 @@ +defmodule TwoBucketTest do + use ExUnit.Case + + # @tag :pending + test "Measure using bucket one of size 3 and bucket two of size 5 - start with bucket one" do + bucket_one = 3 + bucket_two = 5 + goal = 1 + start_bucket = :one + output = TwoBucket.measure(bucket_one, bucket_two, goal, start_bucket) + expected = {:ok, %TwoBucket{bucket_one: goal, bucket_two: 5, moves: 4}} + + assert output == expected + end + + @tag :pending + test "Measure using bucket one of size 3 and bucket two of size 5 - start with bucket two" do + bucket_one = 3 + bucket_two = 5 + goal = 1 + start_bucket = :two + output = TwoBucket.measure(bucket_one, bucket_two, goal, start_bucket) + expected = {:ok, %TwoBucket{bucket_one: 3, bucket_two: goal, moves: 8}} + + assert output == expected + end + + @tag :pending + test "Measure using bucket one of size 7 and bucket two of size 11 - start with bucket one" do + bucket_one = 7 + bucket_two = 11 + goal = 2 + start_bucket = :one + output = TwoBucket.measure(bucket_one, bucket_two, goal, start_bucket) + expected = {:ok, %TwoBucket{bucket_one: goal, bucket_two: 11, moves: 14}} + + assert output == expected + end + + @tag :pending + test "Measure using bucket one of size 7 and bucket two of size 11 - start with bucket two" do + bucket_one = 7 + bucket_two = 11 + goal = 2 + start_bucket = :two + output = TwoBucket.measure(bucket_one, bucket_two, goal, start_bucket) + expected = {:ok, %TwoBucket{bucket_one: 7, bucket_two: goal, moves: 18}} + + assert output == expected + end + + @tag :pending + test "Measure one step using bucket one of size 1 and bucket two of size 3 - start with bucket two" do + bucket_one = 1 + bucket_two = 3 + goal = 3 + start_bucket = :two + output = TwoBucket.measure(bucket_one, bucket_two, goal, start_bucket) + expected = {:ok, %TwoBucket{bucket_one: 0, bucket_two: goal, moves: 1}} + + assert output == expected + end + + @tag :pending + test "Measure using bucket one of size 2 and bucket two of size 3 - start with bucket one and end with bucket two" do + bucket_one = 2 + bucket_two = 3 + goal = 3 + start_bucket = :one + output = TwoBucket.measure(bucket_one, bucket_two, goal, start_bucket) + expected = {:ok, %TwoBucket{bucket_one: 2, bucket_two: goal, moves: 2}} + + assert output == expected + end + + @tag :pending + test "Not possible to reach the goal" do + bucket_one = 6 + bucket_two = 15 + goal = 5 + start_bucket = :one + output = TwoBucket.measure(bucket_one, bucket_two, goal, start_bucket) + expected = {:error, :impossible} + + assert output == expected + end + + @tag :pending + test "With the same buckets but a different goal, then it is possible" do + bucket_one = 6 + bucket_two = 15 + goal = 9 + start_bucket = :one + output = TwoBucket.measure(bucket_one, bucket_two, goal, start_bucket) + expected = {:ok, %TwoBucket{bucket_one: 0, bucket_two: goal, moves: 10}} + + assert output == expected + end + + @tag :pending + test "Goal larger than both buckets is impossible" do + bucket_one = 5 + bucket_two = 7 + goal = 8 + start_bucket = :one + output = TwoBucket.measure(bucket_one, bucket_two, goal, start_bucket) + expected = {:error, :impossible} + + assert output == expected + end +end