Skip to content

Commit

Permalink
Add Two Bucket exercise (#340)
Browse files Browse the repository at this point in the history
  • Loading branch information
blakelewis authored Jan 28, 2024
1 parent dc922fa commit f35805b
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "two-bucket",
"name": "Two Bucket",
"uuid": "05d735e8-d88a-40a6-9aac-4f4dda10e648",
"practices": [],
"prerequisites": [],
"difficulty": 4
}
]
},
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/
17 changes: 17 additions & 0 deletions exercises/practice/two-bucket/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": ["blakelewis"],
"files": {
"solution": [
"two-bucket.rkt"
],
"test": [
"two-bucket-test.rkt"
],
"example": [
".meta/example.rkt"
]
},
"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/"
}
58 changes: 58 additions & 0 deletions exercises/practice/two-bucket/.meta/example.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#lang racket

(provide measure)

(define (validate bucketOne bucketTwo goal)
(if (> goal (max bucketOne bucketTwo))
(error "goal too big")
(let ([g (gcd bucketOne bucketTwo)])
(if (not (zero? (remainder goal g)))
(error "goal not reachable")
null))))

(define (initial-contents bucketOne bucketTwo startBucket)
(let ([one-full (cons bucketOne 0)]
[two-full (cons 0 bucketTwo)])
(cond [(eq? startBucket 'one)
(values one-full two-full)]
[(eq? startBucket 'two)
(values two-full one-full)]
[else (error "invalid start bucket")])))

(define (move-from contents bucketOne bucketTwo)
(let* ([a (car contents)]
[b (cdr contents)]
[a-to-b (min a (- bucketTwo b))]
[b-to-a (min b (- bucketOne a))])
(remove-duplicates
(list (cons 0 b) (cons a 0)
(cons bucketOne b) (cons a bucketTwo)
(cons (- a a-to-b) (+ b a-to-b))
(cons (+ a b-to-a) (- b b-to-a))))))

(define (move-all queue bucketOne bucketTwo visited)
(for ([contents queue])
(set-add! visited contents))
(filter-not (λ (c) (set-member? visited c))
(append-map (λ (c) (move-from c bucketOne bucketTwo)) queue)))

(define (achieves goal queue)
(for/first ([contents queue]
#:when (or (= (car contents) goal)
(= (cdr contents) goal)))
contents))

(define (result moves goal contents)
(if (= (car contents) goal)
(list moves 'one (cdr contents))
(list moves 'two (car contents))))

(define (measure bucketOne bucketTwo goal startBucket)
(validate bucketOne bucketTwo goal)
(let-values ([(start illegal)
(initial-contents bucketOne bucketTwo startBucket)])
(let ([visited (mutable-set illegal)])
(do ([moves 0 (add1 moves)]
[queue (list start) (move-all queue bucketOne bucketTwo visited)]
[winner #f (achieves goal queue)])
(winner (result moves goal winner))))))
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"
48 changes: 48 additions & 0 deletions exercises/practice/two-bucket/two-bucket-test.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#lang racket/base

(require "two-bucket.rkt")

(module+ test
(require rackunit rackunit/text-ui))

(module+ test
(define suite
(test-suite
"two bucket tests"
(test-equal? "Measure using bucket one of size 3 and bucket two of size 5 - start with bucket one"
(measure 3 5 1 'one)
'(4 one 5))

(test-equal? "Measure using bucket one of size 3 and bucket two of size 5 - start with bucket two"
(measure 3 5 1 'two)
'(8 two 3))

(test-equal? "Measure using bucket one of size 7 and bucket two of size 11 - start with bucket one"
(measure 7 11 2 'one)
'(14 one 11))

(test-equal? "Measure using bucket one of size 7 and bucket two of size 11 - start with bucket two"
(measure 7 11 2 'two)
'(18 two 7))

(test-equal? "Measure one step using bucket one of size 1 and bucket two of size 3 - start with bucket two"
(measure 1 3 3 'two)
'(1 two 0))

(test-equal? "Measure using bucket one of size 2 and bucket two of size 3 - start with bucket one and end with bucket two"
(measure 2 3 3 'one)
'(2 two 2))

(test-exn "Not possible to reach the goal"
exn:fail?
(λ () (measure 6 15 5 'two)))

(test-equal? "With the same buckets but a different goal, then it is possible"
(measure 6 15 9 'one)
'(10 two 0))

(test-exn "Goal larger than both buckets is impossible"
exn:fail?
(λ () (measure 5 7 8 'one)))))

(run-tests suite))
6 changes: 6 additions & 0 deletions exercises/practice/two-bucket/two-bucket.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#lang racket

(provide measure)

(define (measure bucketOne bucketTwo goal startBucket)
(error "Not implemented yet"))

0 comments on commit f35805b

Please sign in to comment.