Skip to content

Commit f35805b

Browse files
authored
Add Two Bucket exercise (#340)
1 parent dc922fa commit f35805b

File tree

7 files changed

+220
-0
lines changed

7 files changed

+220
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,14 @@
574574
"practices": [],
575575
"prerequisites": [],
576576
"difficulty": 3
577+
},
578+
{
579+
"slug": "two-bucket",
580+
"name": "Two Bucket",
581+
"uuid": "05d735e8-d88a-40a6-9aac-4f4dda10e648",
582+
"practices": [],
583+
"prerequisites": [],
584+
"difficulty": 4
577585
}
578586
]
579587
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Instructions
2+
3+
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.
4+
5+
There are some rules that your solution must follow:
6+
7+
- You can only do one action at a time.
8+
- There are only 3 possible actions:
9+
1. Pouring one bucket into the other bucket until either:
10+
a) the first bucket is empty
11+
b) the second bucket is full
12+
2. Emptying a bucket and doing nothing to the other.
13+
3. Filling a bucket and doing nothing to the other.
14+
- After an action, you may not arrive at a state where the starting bucket is empty and the other bucket is full.
15+
16+
Your program will take as input:
17+
18+
- the size of bucket one
19+
- the size of bucket two
20+
- the desired number of liters to reach
21+
- which bucket to fill first, either bucket one or bucket two
22+
23+
Your program should determine:
24+
25+
- the total number of actions it should take to reach the desired number of liters, including the first fill of the starting bucket
26+
- which bucket should end up with the desired number of liters - either bucket one or bucket two
27+
- how many liters are left in the other bucket
28+
29+
Note: any time a change is made to either or both buckets counts as one (1) action.
30+
31+
Example:
32+
Bucket one can hold up to 7 liters, and bucket two can hold up to 11 liters.
33+
Let's say at a given step, bucket one is holding 7 liters and bucket two is holding 8 liters (7,8).
34+
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.
35+
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.
36+
37+
Another Example:
38+
Bucket one can hold 3 liters, and bucket two can hold up to 5 liters.
39+
You are told you must start with bucket one.
40+
So your first action is to fill bucket one.
41+
You choose to empty bucket one for your second action.
42+
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.
43+
44+
Written with <3 at [Fullstack Academy][fullstack] by Lindsay Levine.
45+
46+
[fullstack]: https://www.fullstackacademy.com/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"authors": ["blakelewis"],
3+
"files": {
4+
"solution": [
5+
"two-bucket.rkt"
6+
],
7+
"test": [
8+
"two-bucket-test.rkt"
9+
],
10+
"example": [
11+
".meta/example.rkt"
12+
]
13+
},
14+
"blurb": "Given two buckets of different size, demonstrate how to measure an exact number of liters.",
15+
"source": "Water Pouring Problem",
16+
"source_url": "https://demonstrations.wolfram.com/WaterPouringProblem/"
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#lang racket
2+
3+
(provide measure)
4+
5+
(define (validate bucketOne bucketTwo goal)
6+
(if (> goal (max bucketOne bucketTwo))
7+
(error "goal too big")
8+
(let ([g (gcd bucketOne bucketTwo)])
9+
(if (not (zero? (remainder goal g)))
10+
(error "goal not reachable")
11+
null))))
12+
13+
(define (initial-contents bucketOne bucketTwo startBucket)
14+
(let ([one-full (cons bucketOne 0)]
15+
[two-full (cons 0 bucketTwo)])
16+
(cond [(eq? startBucket 'one)
17+
(values one-full two-full)]
18+
[(eq? startBucket 'two)
19+
(values two-full one-full)]
20+
[else (error "invalid start bucket")])))
21+
22+
(define (move-from contents bucketOne bucketTwo)
23+
(let* ([a (car contents)]
24+
[b (cdr contents)]
25+
[a-to-b (min a (- bucketTwo b))]
26+
[b-to-a (min b (- bucketOne a))])
27+
(remove-duplicates
28+
(list (cons 0 b) (cons a 0)
29+
(cons bucketOne b) (cons a bucketTwo)
30+
(cons (- a a-to-b) (+ b a-to-b))
31+
(cons (+ a b-to-a) (- b b-to-a))))))
32+
33+
(define (move-all queue bucketOne bucketTwo visited)
34+
(for ([contents queue])
35+
(set-add! visited contents))
36+
(filter-not (λ (c) (set-member? visited c))
37+
(append-map (λ (c) (move-from c bucketOne bucketTwo)) queue)))
38+
39+
(define (achieves goal queue)
40+
(for/first ([contents queue]
41+
#:when (or (= (car contents) goal)
42+
(= (cdr contents) goal)))
43+
contents))
44+
45+
(define (result moves goal contents)
46+
(if (= (car contents) goal)
47+
(list moves 'one (cdr contents))
48+
(list moves 'two (car contents))))
49+
50+
(define (measure bucketOne bucketTwo goal startBucket)
51+
(validate bucketOne bucketTwo goal)
52+
(let-values ([(start illegal)
53+
(initial-contents bucketOne bucketTwo startBucket)])
54+
(let ([visited (mutable-set illegal)])
55+
(do ([moves 0 (add1 moves)]
56+
[queue (list start) (move-all queue bucketOne bucketTwo visited)]
57+
[winner #f (achieves goal queue)])
58+
(winner (result moves goal winner))))))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[a6f2b4ba-065f-4dca-b6f0-e3eee51cb661]
13+
description = "Measure using bucket one of size 3 and bucket two of size 5 - start with bucket one"
14+
15+
[6c4ea451-9678-4926-b9b3-68364e066d40]
16+
description = "Measure using bucket one of size 3 and bucket two of size 5 - start with bucket two"
17+
18+
[3389f45e-6a56-46d5-9607-75aa930502ff]
19+
description = "Measure using bucket one of size 7 and bucket two of size 11 - start with bucket one"
20+
21+
[fe0ff9a0-3ea5-4bf7-b17d-6d4243961aa1]
22+
description = "Measure using bucket one of size 7 and bucket two of size 11 - start with bucket two"
23+
24+
[0ee1f57e-da84-44f7-ac91-38b878691602]
25+
description = "Measure one step using bucket one of size 1 and bucket two of size 3 - start with bucket two"
26+
27+
[eb329c63-5540-4735-b30b-97f7f4df0f84]
28+
description = "Measure using bucket one of size 2 and bucket two of size 3 - start with bucket one and end with bucket two"
29+
30+
[449be72d-b10a-4f4b-a959-ca741e333b72]
31+
description = "Not possible to reach the goal"
32+
33+
[aac38b7a-77f4-4d62-9b91-8846d533b054]
34+
description = "With the same buckets but a different goal, then it is possible"
35+
36+
[74633132-0ccf-49de-8450-af4ab2e3b299]
37+
description = "Goal larger than both buckets is impossible"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#lang racket/base
2+
3+
(require "two-bucket.rkt")
4+
5+
(module+ test
6+
(require rackunit rackunit/text-ui))
7+
8+
(module+ test
9+
(define suite
10+
(test-suite
11+
"two bucket tests"
12+
(test-equal? "Measure using bucket one of size 3 and bucket two of size 5 - start with bucket one"
13+
(measure 3 5 1 'one)
14+
'(4 one 5))
15+
16+
(test-equal? "Measure using bucket one of size 3 and bucket two of size 5 - start with bucket two"
17+
(measure 3 5 1 'two)
18+
'(8 two 3))
19+
20+
(test-equal? "Measure using bucket one of size 7 and bucket two of size 11 - start with bucket one"
21+
(measure 7 11 2 'one)
22+
'(14 one 11))
23+
24+
(test-equal? "Measure using bucket one of size 7 and bucket two of size 11 - start with bucket two"
25+
(measure 7 11 2 'two)
26+
'(18 two 7))
27+
28+
(test-equal? "Measure one step using bucket one of size 1 and bucket two of size 3 - start with bucket two"
29+
(measure 1 3 3 'two)
30+
'(1 two 0))
31+
32+
(test-equal? "Measure using bucket one of size 2 and bucket two of size 3 - start with bucket one and end with bucket two"
33+
(measure 2 3 3 'one)
34+
'(2 two 2))
35+
36+
(test-exn "Not possible to reach the goal"
37+
exn:fail?
38+
(λ () (measure 6 15 5 'two)))
39+
40+
(test-equal? "With the same buckets but a different goal, then it is possible"
41+
(measure 6 15 9 'one)
42+
'(10 two 0))
43+
44+
(test-exn "Goal larger than both buckets is impossible"
45+
exn:fail?
46+
(λ () (measure 5 7 8 'one)))))
47+
48+
(run-tests suite))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#lang racket
2+
3+
(provide measure)
4+
5+
(define (measure bucketOne bucketTwo goal startBucket)
6+
(error "Not implemented yet"))

0 commit comments

Comments
 (0)