Skip to content

Commit 19823ab

Browse files
authored
add two-bucket (exercism#416)
1 parent 4532007 commit 19823ab

File tree

7 files changed

+272
-0
lines changed

7 files changed

+272
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,14 @@
870870
"practices": [],
871871
"prerequisites": [],
872872
"difficulty": 5
873+
},
874+
{
875+
"slug": "two-bucket",
876+
"name": "Two Bucket",
877+
"uuid": "2eb39d6e-52de-4822-8cd1-ef789c1f5e22",
878+
"practices": [],
879+
"prerequisites": [],
880+
"difficulty": 6
873881
}
874882
]
875883
},
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 initial 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,19 @@
1+
{
2+
"authors": [
3+
"kmarker1101"
4+
],
5+
"files": {
6+
"solution": [
7+
"two-bucket.el"
8+
],
9+
"test": [
10+
"two-bucket-test.el"
11+
],
12+
"example": [
13+
".meta/example.el"
14+
]
15+
},
16+
"blurb": "Given two buckets of different size, demonstrate how to measure an exact number of liters.",
17+
"source": "Water Pouring Problem",
18+
"source_url": "https://demonstrations.wolfram.com/WaterPouringProblem/"
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
;;; two-bucket.el --- Two Bucket (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
(define-error 'goal-not-possible "impossible")
8+
9+
(cl-defstruct (bucket (:constructor make-bucket (capacity name))
10+
(:copier nil))
11+
capacity
12+
(amount 0)
13+
name)
14+
15+
(defun empty (bucket)
16+
(setf (bucket-amount bucket) 0))
17+
18+
(defun fill (bucket)
19+
(setf (bucket-amount bucket) (bucket-capacity bucket)))
20+
21+
(defun fullp (bucket)
22+
(= (bucket-amount bucket) (bucket-capacity bucket)))
23+
24+
(defun overflowingp (bucket)
25+
(when (> (bucket-amount bucket) (bucket-capacity bucket))
26+
(- (bucket-amount bucket) (bucket-capacity bucket))))
27+
28+
(defun transfer (from-bucket to-bucket)
29+
(cl-incf (bucket-amount to-bucket) (bucket-amount from-bucket))
30+
(empty from-bucket)
31+
(when-let ((excess (overflowingp to-bucket)))
32+
(setf (bucket-amount from-bucket) excess)
33+
(fill to-bucket)))
34+
35+
(defun unsolvablep (bucket-one bucket-two goal)
36+
(or (cl-plusp (% goal (cl-gcd bucket-one bucket-two)))
37+
(and (> goal bucket-one) (> goal bucket-two))))
38+
39+
(defun retrieve-results (bucket-one bucket-two goal)
40+
(if (= goal (bucket-amount bucket-one))
41+
(list (bucket-name bucket-one) (bucket-amount bucket-two))
42+
(list (bucket-name bucket-two) (bucket-amount bucket-one))))
43+
44+
(defun iterate-through-puzzle (start-bucket other-bucket goal)
45+
(fill start-bucket)
46+
(cl-loop for moves from 1
47+
until (or (= goal (bucket-amount start-bucket))
48+
(= goal (bucket-amount other-bucket)))
49+
do (cond
50+
((= goal (bucket-capacity other-bucket)) (fill other-bucket))
51+
((fullp other-bucket) (empty other-bucket))
52+
((zerop (bucket-amount start-bucket)) (fill start-bucket))
53+
(t (transfer start-bucket other-bucket)))
54+
finally (return (cons moves (retrieve-results start-bucket other-bucket goal)))))
55+
56+
(defun measure (bucket-one bucket-two goal start-bucket)
57+
(if (unsolvablep bucket-one bucket-two goal)
58+
(signal 'goal-not-possible nil)
59+
(let* ((first-bucket (make-bucket bucket-one 'one))
60+
(second-bucket (make-bucket bucket-two 'two))
61+
(results (if (eq start-bucket 'one)
62+
(iterate-through-puzzle first-bucket second-bucket goal)
63+
(iterate-through-puzzle second-bucket first-bucket goal))))
64+
(cons (car results) (cdr results)))))
65+
66+
67+
(provide 'two-bucket)
68+
;;; two-bucket.el ends here
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,77 @@
1+
;;; two-bucket-test.el --- Two Bucket (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
8+
(load-file "two-bucket.el")
9+
(declare-function measure "two-bucket.el" (bucket-one bucket-two goal start-bucket))
10+
11+
12+
(ert-deftest measure-using-bucket-one-of-size-3-and-bucket-two-of-size-5-start-with-bucket-one ()
13+
;; Function under test: measure
14+
;; Input: {"bucketOne":3,"bucketTwo":5,"goal":1,"startBucket":"one"}
15+
;; Expected: {"moves":4,"goalBucket":"one","otherBucket":5}
16+
(should (equal (measure 3 5 1 'one) '(4 one 5))))
17+
18+
19+
(ert-deftest measure-using-bucket-one-of-size-3-and-bucket-two-of-size-5-start-with-bucket-two ()
20+
;; Function under test: measure
21+
;; Input: {"bucketOne":3,"bucketTwo":5,"goal":1,"startBucket":"two"}
22+
;; Expected: {"moves":8,"goalBucket":"two","otherBucket":3}
23+
(should (equal (measure 3 5 1 'two) '(8 two 3))))
24+
25+
26+
(ert-deftest measure-using-bucket-one-of-size-7-and-bucket-two-of-size-11-start-with-bucket-one ()
27+
;; Function under test: measure
28+
;; Input: {"bucketOne":7,"bucketTwo":11,"goal":2,"startBucket":"one"}
29+
;; Expected: {"moves":14,"goalBucket":"one","otherBucket":11}
30+
(should (equal (measure 7 11 2 'one) '(14 one 11))))
31+
32+
33+
(ert-deftest measure-using-bucket-one-of-size-7-and-bucket-two-of-size-11-start-with-bucket-two ()
34+
;; Function under test: measure
35+
;; Input: {"bucketOne":7,"bucketTwo":11,"goal":2,"startBucket":"two"}
36+
;; Expected: {"moves":18,"goalBucket":"two","otherBucket":7}
37+
(should (equal (measure 7 11 2 'two) '(18 two 7))))
38+
39+
40+
(ert-deftest measure-one-step-using-bucket-one-of-size-1-and-bucket-two-of-size-3-start-with-bucket-two ()
41+
;; Function under test: measure
42+
;; Input: {"bucketOne":1,"bucketTwo":3,"goal":3,"startBucket":"two"}
43+
;; Expected: {"moves":1,"goalBucket":"two","otherBucket":0}
44+
(should (equal (measure 1 3 3 'two) '(1 two 0))))
45+
46+
47+
(ert-deftest measure-using-bucket-one-of-size-2-and-bucket-two-of-size-3-start-with-bucket-one-and-end-with-bucket-two ()
48+
;; Function under test: measure
49+
;; Input: {"bucketOne":2,"bucketTwo":3,"goal":3,"startBucket":"one"}
50+
;; Expected: {"moves":2,"goalBucket":"two","otherBucket":2}
51+
(should (equal (measure 2 3 3 'one) '(2 two 2))))
52+
53+
54+
(ert-deftest not-possible-to-reach-the-goal ()
55+
;; Function under test: measure
56+
;; Input: {"bucketOne":6,"bucketTwo":15,"goal":5,"startBucket":"one"}
57+
;; Expected: {"error":"impossible"}
58+
(should-error (measure 6 15 5 'one) :type 'goal-not-possible))
59+
60+
61+
(ert-deftest with-the-same-buckets-but-a-different-goal-then-it-is-possible ()
62+
;; Function under test: measure
63+
;; Input: {"bucketOne":6,"bucketTwo":15,"goal":9,"startBucket":"one"}
64+
;; Expected: {"moves":10,"goalBucket":"two","otherBucket":0}
65+
(should (equal (measure 6 15 9 'one) '(10 two 0))))
66+
67+
68+
(ert-deftest goal-larger-than-both-buckets-is-impossible ()
69+
;; Function under test: measure
70+
;; Input: {"bucketOne":5,"bucketTwo":7,"goal":8,"startBucket":"one"}
71+
;; Expected: {"error":"impossible"}
72+
(should-error (measure 5 7 8 'one) :type 'goal-not-possible))
73+
74+
75+
(provide 'two-bucket-test)
76+
;;; two-bucket-test.el ends here
77+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
;;; two-bucket.el --- Two Bucket (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
8+
(define-error 'goal-not-possible
9+
(error "Delete this S-Expression and write your own implementation"))
10+
11+
(defun measure (bucket-one bucket-two goal start-bucket)
12+
(error "Delete this S-Expression and write your own implementation"))
13+
14+
15+
(provide 'two-bucket)
16+
;;; two-bucket.el ends here
17+

0 commit comments

Comments
 (0)