Skip to content

Commit 2efa3a3

Browse files
authored
Add Two Bucket Exercise (#636)
1 parent 3701616 commit 2efa3a3

File tree

7 files changed

+482
-0
lines changed

7 files changed

+482
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,14 @@
12191219
"practices": [],
12201220
"prerequisites": [],
12211221
"difficulty": 3
1222+
},
1223+
{
1224+
"slug": "two-bucket",
1225+
"name": "Two Bucket",
1226+
"uuid": "516250c6-6e1b-4710-b2b9-6bef1716c6d8",
1227+
"practices": [],
1228+
"prerequisites": [],
1229+
"difficulty": 5
12221230
}
12231231
]
12241232
},
Lines changed: 46 additions & 0 deletions
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/
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"tomasnorre"
4+
],
5+
"files": {
6+
"solution": [
7+
"TwoBucket.php"
8+
],
9+
"test": [
10+
"TwoBucketTest.php"
11+
],
12+
"example": [
13+
".meta/example.php"
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+
}
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
<?php
2+
3+
/*
4+
* By adding type hints and enabling strict type checking, code can become
5+
* easier to read, self-documenting and reduce the number of potential bugs.
6+
* By default, type declarations are non-strict, which means they will attempt
7+
* to change the original type to match the type specified by the
8+
* type-declaration.
9+
*
10+
* In other words, if you pass a string to a function requiring a float,
11+
* it will attempt to convert the string value to a float.
12+
*
13+
* To enable strict mode, a single declare directive must be placed at the top
14+
* of the file.
15+
* This means that the strictness of typing is configured on a per-file basis.
16+
* This directive not only affects the type declarations of parameters, but also
17+
* a function's return type.
18+
*
19+
* For more info review the Concept on strict type checking in the PHP track
20+
* <link>.
21+
*
22+
* To disable strict typing, comment out the directive below.
23+
*/
24+
25+
declare(strict_types=1);
26+
27+
class TwoBucket
28+
{
29+
private int $goal;
30+
private array $buckets;
31+
32+
public function solve(int $sizeBucketOne, int $sizeBucketTwo, int $goal, string $startBucket): Solution
33+
{
34+
$this->goal = $goal;
35+
$this->buckets = [new Bucket('one', $sizeBucketOne), new Bucket('two', $sizeBucketTwo)];
36+
37+
if ($startBucket === 'two') {
38+
$this->buckets = array_reverse($this->buckets);
39+
}
40+
41+
$this->validate();
42+
$this->first()->empty();
43+
$this->second()->empty();
44+
$moves = 0;
45+
46+
$this->first()->fill();
47+
$moves++;
48+
49+
if ($this->second()->getSize() === $this->goal) {
50+
$this->second()->fill();
51+
$moves++;
52+
}
53+
54+
while (true) {
55+
if ($this->first()->getAmount() === $this->goal) {
56+
return new Solution(
57+
$moves,
58+
$this->first()->getName(),
59+
$this->second()->getAmount()
60+
);
61+
}
62+
63+
if ($this->second()->getAmount() === $this->goal) {
64+
return new Solution(
65+
$moves,
66+
$this->second()->getName(),
67+
$this->first()->getAmount()
68+
);
69+
}
70+
71+
if ($this->first()->isEmpty()) {
72+
$this->first()->fill();
73+
} elseif ($this->second()->isFull()) {
74+
$this->second()->empty();
75+
} else {
76+
$this->first()->pourInto($this->second());
77+
}
78+
79+
$moves++;
80+
}
81+
}
82+
83+
private function first(): Bucket
84+
{
85+
return $this->buckets[0];
86+
}
87+
88+
private function second(): Bucket
89+
{
90+
return $this->buckets[1];
91+
}
92+
93+
private function validate(): void
94+
{
95+
if ($this->goal > max($this->first()->getSize(), $this->second()->getSize())) {
96+
throw new \RuntimeException('Goal is bigger than the largest bucket.');
97+
}
98+
99+
if ($this->goal % $this->greatestCommonDivisor($this->first()->getSize(), $this->second()->getSize()) !== 0) {
100+
throw new \RuntimeException('Goal must be a multiple of the GCD of the sizes of the two buckets.');
101+
}
102+
}
103+
104+
private function greatestCommonDivisor($a, $b)
105+
{
106+
return $b === 0 ? $a : $this->greatestCommonDivisor($b, $a % $b);
107+
}
108+
}
109+
110+
class Solution
111+
{
112+
public function __construct(
113+
public int $numberOfActions,
114+
public string $nameOfBucketWithDesiredLiters,
115+
public int $litersLeftInOtherBucket,
116+
) {
117+
}
118+
}
119+
120+
class Bucket
121+
{
122+
private string $name;
123+
private int $size;
124+
private int $amount;
125+
126+
public function __construct(string $name, int $size)
127+
{
128+
$this->name = $name;
129+
$this->size = $size;
130+
$this->amount = 0;
131+
}
132+
133+
public function getName(): string
134+
{
135+
return $this->name;
136+
}
137+
138+
public function getSize(): int
139+
{
140+
return $this->size;
141+
}
142+
143+
public function getAmount(): int
144+
{
145+
return $this->amount;
146+
}
147+
148+
public function getAvailable(): int
149+
{
150+
return $this->size - $this->amount;
151+
}
152+
153+
public function isFull(): bool
154+
{
155+
return $this->amount === $this->size;
156+
}
157+
158+
public function isEmpty(): bool
159+
{
160+
return $this->amount === 0;
161+
}
162+
163+
public function fill(): void
164+
{
165+
$this->amount = $this->size;
166+
}
167+
168+
public function empty(): void
169+
{
170+
$this->amount = 0;
171+
}
172+
173+
public function pourInto(Bucket $other): void
174+
{
175+
$quantity = min($this->amount, $other->getAvailable());
176+
$this->amount -= $quantity;
177+
$other->add($quantity);
178+
}
179+
180+
private function add($quantity): void
181+
{
182+
$this->amount += $quantity;
183+
}
184+
}
Lines changed: 37 additions & 0 deletions
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"
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* By adding type hints and enabling strict type checking, code can become
5+
* easier to read, self-documenting and reduce the number of potential bugs.
6+
* By default, type declarations are non-strict, which means they will attempt
7+
* to change the original type to match the type specified by the
8+
* type-declaration.
9+
*
10+
* In other words, if you pass a string to a function requiring a float,
11+
* it will attempt to convert the string value to a float.
12+
*
13+
* To enable strict mode, a single declare directive must be placed at the top
14+
* of the file.
15+
* This means that the strictness of typing is configured on a per-file basis.
16+
* This directive not only affects the type declarations of parameters, but also
17+
* a function's return type.
18+
*
19+
* For more info review the Concept on strict type checking in the PHP track
20+
* <link>.
21+
*
22+
* To disable strict typing, comment out the directive below.
23+
*/
24+
25+
declare(strict_types=1);
26+
27+
class TwoBucket
28+
{
29+
public function solve(int $sizeBucketOne, int $sizeBucketTwo, int $goal, string $startBucket)
30+
{
31+
throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__));
32+
}
33+
}

0 commit comments

Comments
 (0)