Skip to content

Commit

Permalink
Add Two Bucket exercise (#1215)
Browse files Browse the repository at this point in the history
* Add Two Bucket exercise

* Update exercises/practice/two-bucket/.meta/examples/success-standard/package.yaml

Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com>

* Update exercises/practice/two-bucket/package.yaml

Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com>

* Incorporate review comments

---------

Co-authored-by: Erik Schierboom <erik_schierboom@hotmail.com>
  • Loading branch information
tofische and ErikSchierboom authored Mar 27, 2024
1 parent b060e70 commit 89f9523
Show file tree
Hide file tree
Showing 11 changed files with 325 additions and 0 deletions.
12 changes: 12 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,18 @@
"mutable_state"
]
},
{
"slug": "two-bucket",
"name": "Two Bucket",
"uuid": "7f741717-13dc-43dd-a20f-9f8b0ad02acc",
"practices": [],
"prerequisites": [],
"difficulty": 7,
"topics": [
"algorithms",
"logic"
]
},
{
"slug": "zebra-puzzle",
"name": "Zebra Puzzle",
Expand Down
4 changes: 4 additions & 0 deletions exercises/practice/two-bucket/.docs/instructions.append.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Note

For the sake of simplicity you can assume that the starting bucket to be filled first
is always bucket one - it is enough just to swap the buckets if it shall be bucket two.
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/
23 changes: 23 additions & 0 deletions exercises/practice/two-bucket/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"authors": [
"tofische"
],
"files": {
"solution": [
"src/TwoBucket.hs",
"package.yaml"
],
"test": [
"test/Tests.hs"
],
"example": [
".meta/examples/success-standard/src/TwoBucket.hs"
],
"invalidator": [
"stack.yaml"
]
},
"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/"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: two-bucket

dependencies:
- base
- mtl

library:
exposed-modules: TwoBucket
source-dirs: src
ghc-options: -Wall
# dependencies:
# - foo # List here the packages you
# - bar # want to use in your solution.

tests:
test:
main: Tests.hs
source-dirs: test
dependencies:
- two-bucket
- hspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
module TwoBucket (measure) where

import Control.Monad.State ( evalState, MonadState(put, get), StateT )
import qualified Control.Monad.Identity as Data.Functor.Identity
import Data.Maybe (mapMaybe)

-- Current status of the buckets, or their initial capacity
type Buckets = (Int, Int)
-- Sequence of moves
type Moves = [Buckets]
-- Solution candidates
type Candidates = [Moves]

measure :: Buckets -> Int -> Maybe (Int, Buckets)
measure capacity target =
if null solution
then Nothing
else let sol = head solution in Just (length sol - 1, head sol)
where
solution = evalState (solve capacity target) [[(0, 0)]]

solve :: Buckets -> Int -> StateT Candidates Data.Functor.Identity.Identity Candidates
solve (c1, c2) target = solve'
where
solve' = do
oldCandidates <- get
if any isTarget oldCandidates
then return $ filter isTarget oldCandidates
else do
let newCandidates = oldCandidates >>= expand
if null newCandidates
then return []
else do
put newCandidates
solve'
isTarget [] = False
isTarget ((s1, s2):_) = s1 == target || s2 == target

expand :: Moves -> Candidates
expand candidate = allMoves (head candidate) >>= prependMove candidate

allMoves :: Buckets -> Moves
allMoves buckets = mapMaybe ($ buckets) [move12, move21, empty1, empty2, fill1, fill2]

prependMove :: Moves -> Buckets -> [Moves]
prependMove moves buckets = [buckets:moves | buckets `notElem` moves]

-- Pour 1st bucket into the 2nd one
move12 (s1, s2)
| s1 == 0 = Nothing
| s12 == c2 = Nothing
| s12 < c2 = Just (0, s12)
| otherwise = Just (s12 - c2, c2)
where
s12 = s1 + s2

-- Pour 2nd bucket into the 1st one
move21 (s1, s2)
| s2 == 0 = Nothing
| s12 < c1 = Just (s12, 0)
| otherwise = Just (c1, s12 - c1)
where
s12 = s1 + s2

-- Empty 1st bucket
empty1 (s1, s2)
| s1 == 0 = Nothing
| s2 == c2 = Nothing
| otherwise = Just (0, s2)

-- Empty 2nd bucket
empty2 (s1, s2)
| s2 == 0 = Nothing
| otherwise = Just (s1, 0)

-- Fill 1st bucket
fill1 (s1, s2)
| s1 == c1 = Nothing
| otherwise = Just (c1, s2)

-- Fill 2nd bucket
fill2 (s1, s2)
| s2 == c2 = Nothing
| s1 == 0 = Nothing
| otherwise = Just (s1, c2)
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"
22 changes: 22 additions & 0 deletions exercises/practice/two-bucket/package.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: two-bucket
version: 1.0.0.0

dependencies:
- base
- mtl

library:
exposed-modules: TwoBucket
source-dirs: src
ghc-options: -Wall
# dependencies:
# - foo # List here the packages you
# - bar # want to use in your solution.

tests:
test:
main: Tests.hs
source-dirs: test
dependencies:
- two-bucket
- hspec
4 changes: 4 additions & 0 deletions exercises/practice/two-bucket/src/TwoBucket.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module TwoBucket (measure) where

measure :: (Int, Int) -> Int -> Maybe (Int, (Int, Int))
measure capacity target = error "You need to implement this function."
1 change: 1 addition & 0 deletions exercises/practice/two-bucket/stack.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
resolver: lts-20.18
70 changes: 70 additions & 0 deletions exercises/practice/two-bucket/test/Tests.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{-# LANGUAGE RecordWildCards #-}

import Data.Foldable (for_)
import Data.Tuple (swap)
import Test.Hspec (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFailFast, defaultConfig, hspecWith)

import TwoBucket (measure)

main :: IO ()
main = hspecWith defaultConfig {configFailFast = True} specs

specs :: Spec
specs = describe "measure" $ for_ cases test
where
test Case{..} = it description $ measure capacity target `shouldBe` expected

data Case = Case { description :: String
, capacity :: (Int, Int)
, target :: Int
, expected :: Maybe (Int, (Int, Int))
}

cases :: [Case]
cases = [ Case { description = "Measure using bucket one of size 3 and bucket two of size 5 - start with bucket one"
, capacity = (3, 5)
, target = 1
, expected = Just (4, (1, 5))
}
, Case { description = "Measure using bucket one of size 3 and bucket two of size 5 - start with bucket two"
, capacity = swap (3, 5)
, target = 1
, expected = Just (8, swap (3, 1))
}
, Case { description = "Measure using bucket one of size 7 and bucket two of size 11 - start with bucket one"
, capacity = (7, 11)
, target = 2
, expected = Just (14, (2, 11))
}
, Case { description = "Measure using bucket one of size 7 and bucket two of size 11 - start with bucket two"
, capacity = swap (7, 11)
, target = 2
, expected = Just (18, swap (7, 2))
}
, Case { description = "Measure one step using bucket one of size 1 and bucket two of size 3 - start with bucket two"
, capacity = swap (1, 3)
, target = 3
, expected = Just (1, swap (0, 3))
}
, Case { description = "Measure using bucket one of size 2 and bucket two of size 3 - start with bucket one and end with bucket two"
, capacity = (2, 3)
, target = 3
, expected = Just (2, (2, 3))
}
, Case { description = "Not possible to reach the goal"
, capacity = (6, 15)
, target = 5
, expected = Nothing
}
, Case { description = "With the same buckets but a different goal, then it is possible"
, capacity = (6, 15)
, target = 9
, expected = Just (10, (0, 9))
}
, Case { description = "Goal larger than both buckets is impossible"
, capacity = (5, 7)
, target = 8
, expected = Nothing
}
]

0 comments on commit 89f9523

Please sign in to comment.