From 809285fa117dddb7ed4cc64792b45d06a735b313 Mon Sep 17 00:00:00 2001 From: Tomas Norre Mikkelsen Date: Thu, 22 Feb 2024 22:02:01 +0100 Subject: [PATCH 1/6] Add Two Bucket Exercise --- config.json | 9 + .../practice/two-bucket/.docs/instructions.md | 46 +++++ .../practice/two-bucket/.meta/config.json | 19 +++ .../practice/two-bucket/.meta/example.php | 156 +++++++++++++++++ .../practice/two-bucket/.meta/tests.toml | 37 +++++ exercises/practice/two-bucket/TwoBucket.php | 46 +++++ .../practice/two-bucket/TwoBucketTest.php | 157 ++++++++++++++++++ 7 files changed, 470 insertions(+) create mode 100644 exercises/practice/two-bucket/.docs/instructions.md create mode 100644 exercises/practice/two-bucket/.meta/config.json create mode 100644 exercises/practice/two-bucket/.meta/example.php create mode 100644 exercises/practice/two-bucket/.meta/tests.toml create mode 100644 exercises/practice/two-bucket/TwoBucket.php create mode 100644 exercises/practice/two-bucket/TwoBucketTest.php diff --git a/config.json b/config.json index 9bd0ab9b..0a4a2df8 100644 --- a/config.json +++ b/config.json @@ -1195,6 +1195,7 @@ "practices": [], "prerequisites": [], "difficulty": 3 +<<<<<<< HEAD }, { "slug": "rotational-cipher", @@ -1203,6 +1204,14 @@ "practices": [], "prerequisites": [], "difficulty": 3 + }, + { + "slug": "two-bucket", + "name": "Two Bucket", + "uuid": "516250c6-6e1b-4710-b2b9-6bef1716c6d8", + "practices": [], + "prerequisites": [], + "difficulty": 5 } ] }, diff --git a/exercises/practice/two-bucket/.docs/instructions.md b/exercises/practice/two-bucket/.docs/instructions.md new file mode 100644 index 00000000..7249deb3 --- /dev/null +++ b/exercises/practice/two-bucket/.docs/instructions.md @@ -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/ diff --git a/exercises/practice/two-bucket/.meta/config.json b/exercises/practice/two-bucket/.meta/config.json new file mode 100644 index 00000000..c43f032c --- /dev/null +++ b/exercises/practice/two-bucket/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "tomasnorre" + ], + "files": { + "solution": [ + "TwoBucket.php" + ], + "test": [ + "TwoBucketTest.php" + ], + "example": [ + ".meta/example.php" + ] + }, + "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/" +} diff --git a/exercises/practice/two-bucket/.meta/example.php b/exercises/practice/two-bucket/.meta/example.php new file mode 100644 index 00000000..76950c25 --- /dev/null +++ b/exercises/practice/two-bucket/.meta/example.php @@ -0,0 +1,156 @@ +goal = $goal; + $this->buckets = [new Bucket('one', $sizeBucketOne), new Bucket('two', $sizeBucketTwo)]; + + if ($startBucket === 'two') { + $this->buckets = array_reverse($this->buckets); + } + + $this->validate(); + } + + public function solve(): array + { + $this->first()->empty(); + $this->second()->empty(); + $moves = 0; + + $this->first()->fill(); + $moves++; + + if ($this->second()->getSize() === $this->goal) { + $this->second()->fill(); + $moves++; + } + + while (true) { + if ($this->first()->getAmount() === $this->goal) { + return [ + 'moves' => $moves, + 'goalBucket' => $this->first()->getName(), + 'otherBucket' => $this->second()->getAmount(), + ]; + } + + if ($this->second()->getAmount() === $this->goal) { + return [ + 'moves' => $moves, + 'goalBucket' => $this->second()->getName(), + 'otherBucket' => $this->first()->getAmount(), + ]; + } + + if ($this->first()->isEmpty()) { + $this->first()->fill(); + } elseif ($this->second()->isFull()) { + $this->second()->empty(); + } else { + $this->first()->pourInto($this->second()); + } + + $moves++; + } + } + + private function first(): Bucket + { + return $this->buckets[0]; + } + + private function second(): Bucket + { + return $this->buckets[1]; + } + + private function validate(): void + { + if ($this->goal > max($this->first()->getSize(), $this->second()->getSize())) { + throw new \RuntimeException('Goal is bigger than the largest bucket.'); + } + + if ($this->goal % $this->greatestCommonDivisor($this->first()->getSize(), $this->second()->getSize()) !== 0) { + throw new \RuntimeException('Goal must be a multiple of the GCD of the sizes of the two buckets.'); + } + } + + private function greatestCommonDivisor($a, $b) + { + return $b === 0 ? $a : $this->greatestCommonDivisor($b, $a % $b); + } +} + +class Bucket +{ + private string $name; + private int $size; + private int $amount; + + public function __construct(string $name, int $size) + { + $this->name = $name; + $this->size = $size; + $this->amount = 0; + } + + public function getName(): string + { + return $this->name; + } + + public function getSize(): int + { + return $this->size; + } + + public function getAmount(): int + { + return $this->amount; + } + + public function getAvailable(): int + { + return $this->size - $this->amount; + } + + public function isFull(): bool + { + return $this->amount === $this->size; + } + + public function isEmpty(): bool + { + return $this->amount === 0; + } + + public function fill(): void + { + $this->amount = $this->size; + } + + public function empty(): void + { + $this->amount = 0; + } + + public function pourInto(Bucket $other): void + { + $quantity = min($this->amount, $other->getAvailable()); + $this->amount -= $quantity; + $other->add($quantity); + } + + private function add($quantity): void + { + $this->amount += $quantity; + } +} diff --git a/exercises/practice/two-bucket/.meta/tests.toml b/exercises/practice/two-bucket/.meta/tests.toml new file mode 100644 index 00000000..d6ff02f5 --- /dev/null +++ b/exercises/practice/two-bucket/.meta/tests.toml @@ -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" diff --git a/exercises/practice/two-bucket/TwoBucket.php b/exercises/practice/two-bucket/TwoBucket.php new file mode 100644 index 00000000..a9728d6c --- /dev/null +++ b/exercises/practice/two-bucket/TwoBucket.php @@ -0,0 +1,46 @@ +. + * + * To disable strict typing, comment out the directive below. + */ + +declare(strict_types=1); + +class TwoBucket +{ + public function __construct(int $sizeBucketOne, int $sizeBucketTwo, int $goal, string $startBucket) + { + throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__)); + } + + public function solve(): array + { + throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__)); + } +} + +class Bucket +{ + public function __construct(string $name, int $size) + { + throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__)); + } +} diff --git a/exercises/practice/two-bucket/TwoBucketTest.php b/exercises/practice/two-bucket/TwoBucketTest.php new file mode 100644 index 00000000..6bfc45c6 --- /dev/null +++ b/exercises/practice/two-bucket/TwoBucketTest.php @@ -0,0 +1,157 @@ +solve(); + + $this->assertEquals(4, $result['moves']); + $this->assertEquals('one', $result['goalBucket']); + $this->assertEquals(5, $result['otherBucket']); + } + + /** + * uuid: 6c4ea451-9678-4926-b9b3-68364e066d40 + */ + public function testMeasureUsingBucketOneOfSize3AndBucketTwoOfSize5StartWithBucketTwo(): void + { + $buckOne = 3; + $buckTwo = 5; + $goal = 1; + $starterBuck = 'two'; + $twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck); + $result = $twoBucket->solve(); + + $this->assertEquals(8, $result['moves']); + $this->assertEquals('two', $result['goalBucket']); + $this->assertEquals(3, $result['otherBucket']); + } + + /** + * uuid: 3389f45e-6a56-46d5-9607-75aa930502ff + */ + public function testMeasureUsingBucketOneOfSize7AndBucketTwoOfSize11StartWithBucketOne(): void + { + $buckOne = 7; + $buckTwo = 11; + $goal = 2; + $starterBuck = 'one'; + $twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck); + $result = $twoBucket->solve(); + + $this->assertEquals(14, $result['moves']); + $this->assertEquals('one', $result['goalBucket']); + $this->assertEquals(11, $result['otherBucket']); + } + + /** + * uuid: fe0ff9a0-3ea5-4bf7-b17d-6d4243961aa1 + */ + public function testMeasureUsingBucketOneOfSize7AndBucketTwoOfSize11StartWithBucketTwo(): void + { + $buckOne = 7; + $buckTwo = 11; + $goal = 2; + $starterBuck = 'two'; + $twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck); + $result = $twoBucket->solve(); + + $this->assertEquals(18, $result['moves']); + $this->assertEquals('two', $result['goalBucket']); + $this->assertEquals(7, $result['otherBucket']); + } + + /** + * uuid: 0ee1f57e-da84-44f7-ac91-38b878691602 + */ + public function testMeasureOneStepUsingBucketOneOfSize1AndBucketTwoOfSize3StartWithBucketTwo(): void + { + $twoBucket = new TwoBucket(1, 3, 3, 'two'); + $result = $twoBucket->solve(); + + $this->assertEquals(1, $result['moves']); + $this->assertEquals('two', $result['goalBucket']); + $this->assertEquals(0, $result['otherBucket']); + } + + /** + * uuid: eb329c63-5540-4735-b30b-97f7f4df0f84 + */ + public function testMeasureUsingBucketOneOfSize2AndBucketTwoOfSize3StartWithBucketOneAndEndWithBucketTwo(): void + { + $twoBucket = new TwoBucket(2, 3, 3, 'one'); + $result = $twoBucket->solve(); + + $this->assertEquals(2, $result['moves']); + $this->assertEquals('two', $result['goalBucket']); + $this->assertEquals(2, $result['otherBucket']); + } + + /** + * uuid: 449be72d-b10a-4f4b-a959-ca741e333b72 + */ + public function testReachabilityNotPossibleToReachGoalStartWithBucketOne(): void + { + $buckOne = 6; + $buckTwo = 15; + $this->expectException(Exception::class); + $twoBucket = new TwoBucket($buckOne, $buckTwo, 5, 'one'); + $twoBucket->solve(); + } + + /** + * uuid: aac38b7a-77f4-4d62-9b91-8846d533b054 + */ + public function testReachabilityNotPossibleToReachGoalStartWithBucketOneAndEndWithBucketTwo(): void + { + $twoBucket = new TwoBucket(6, 15, 9, 'one'); + $result = $twoBucket->solve(); + + $this->assertEquals(10, $result['moves']); + $this->assertEquals('two', $result['goalBucket']); + $this->assertEquals(0, $result['otherBucket']); + } + + /** + * uuid: aac38b7a-77f4-4d62-9b91-8846d533b054 + */ + public function testWithSameBucketsButDifferentGoalItIsPossible(): void + { + $buckOne = 6; + $buckTwo = 15; + $goal = 9; + $starterBuck = 'one'; + $twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck); + $result = $twoBucket->solve(); + + $this->assertEquals(10, $result['moves']); + $this->assertEquals('two', $result['goalBucket']); + $this->assertEquals(0, $result['otherBucket']); + } + + /** + * uuid: 74633132-0ccf-49de-8450-af4ab2e3b299 + */ + public function testGoalLargerThanBothBucketsIsImpossible(): void + { + $this->expectException(Exception::class); + $twoBucket = new TwoBucket(5, 7, 8, 'one'); + $twoBucket->solve(); + } +} From 268a243cb495ef4eb414f9dd5a91c630182d3abb Mon Sep 17 00:00:00 2001 From: Tomas Norre Mikkelsen Date: Thu, 22 Feb 2024 22:24:59 +0100 Subject: [PATCH 2/6] Remove extra line in config.json --- config.json | 1 - 1 file changed, 1 deletion(-) diff --git a/config.json b/config.json index 0a4a2df8..ac86d4de 100644 --- a/config.json +++ b/config.json @@ -1195,7 +1195,6 @@ "practices": [], "prerequisites": [], "difficulty": 3 -<<<<<<< HEAD }, { "slug": "rotational-cipher", From d950d129ecfad51acc94bb6caff153b022c9af78 Mon Sep 17 00:00:00 2001 From: Tomas Norre Mikkelsen Date: Fri, 23 Feb 2024 14:17:36 +0100 Subject: [PATCH 3/6] Switch to resturn object instead of array --- .../practice/two-bucket/.meta/example.php | 23 ++++----- exercises/practice/two-bucket/TwoBucket.php | 10 +--- .../practice/two-bucket/TwoBucketTest.php | 48 +++++++++---------- 3 files changed, 37 insertions(+), 44 deletions(-) diff --git a/exercises/practice/two-bucket/.meta/example.php b/exercises/practice/two-bucket/.meta/example.php index 76950c25..b43b72d5 100644 --- a/exercises/practice/two-bucket/.meta/example.php +++ b/exercises/practice/two-bucket/.meta/example.php @@ -6,6 +6,9 @@ class TwoBucket { private int $goal; private array $buckets; + public int $moves; + public string $goalBucket; + public int $otherBucket; public function __construct(int $sizeBucketOne, int $sizeBucketTwo, int $goal, string $startBucket) { @@ -19,7 +22,7 @@ public function __construct(int $sizeBucketOne, int $sizeBucketTwo, int $goal, s $this->validate(); } - public function solve(): array + public function solve(): self { $this->first()->empty(); $this->second()->empty(); @@ -35,19 +38,17 @@ public function solve(): array while (true) { if ($this->first()->getAmount() === $this->goal) { - return [ - 'moves' => $moves, - 'goalBucket' => $this->first()->getName(), - 'otherBucket' => $this->second()->getAmount(), - ]; + $this->moves = $moves; + $this->goalBucket = $this->first()->getName(); + $this->otherBucket = $this->second()->getAmount(); + return $this; } if ($this->second()->getAmount() === $this->goal) { - return [ - 'moves' => $moves, - 'goalBucket' => $this->second()->getName(), - 'otherBucket' => $this->first()->getAmount(), - ]; + $this->moves = $moves; + $this->goalBucket = $this->second()->getName(); + $this->otherBucket = $this->first()->getAmount(); + return $this; } if ($this->first()->isEmpty()) { diff --git a/exercises/practice/two-bucket/TwoBucket.php b/exercises/practice/two-bucket/TwoBucket.php index a9728d6c..7ed53c5c 100644 --- a/exercises/practice/two-bucket/TwoBucket.php +++ b/exercises/practice/two-bucket/TwoBucket.php @@ -31,15 +31,7 @@ public function __construct(int $sizeBucketOne, int $sizeBucketTwo, int $goal, s throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__)); } - public function solve(): array - { - throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__)); - } -} - -class Bucket -{ - public function __construct(string $name, int $size) + public function solve(): self { throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__)); } diff --git a/exercises/practice/two-bucket/TwoBucketTest.php b/exercises/practice/two-bucket/TwoBucketTest.php index 6bfc45c6..dd9fc91f 100644 --- a/exercises/practice/two-bucket/TwoBucketTest.php +++ b/exercises/practice/two-bucket/TwoBucketTest.php @@ -21,9 +21,9 @@ public function testMeasureUsingBucketOneOfSize3AndBucketTwoOfSize5StartWithBuck $twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck); $result = $twoBucket->solve(); - $this->assertEquals(4, $result['moves']); - $this->assertEquals('one', $result['goalBucket']); - $this->assertEquals(5, $result['otherBucket']); + $this->assertEquals(4, $result->moves); + $this->assertEquals('one', $result->goalBucket); + $this->assertEquals(5, $result->otherBucket); } /** @@ -38,9 +38,9 @@ public function testMeasureUsingBucketOneOfSize3AndBucketTwoOfSize5StartWithBuck $twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck); $result = $twoBucket->solve(); - $this->assertEquals(8, $result['moves']); - $this->assertEquals('two', $result['goalBucket']); - $this->assertEquals(3, $result['otherBucket']); + $this->assertEquals(8, $result->moves); + $this->assertEquals('two', $result->goalBucket); + $this->assertEquals(3, $result->otherBucket); } /** @@ -55,9 +55,9 @@ public function testMeasureUsingBucketOneOfSize7AndBucketTwoOfSize11StartWithBuc $twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck); $result = $twoBucket->solve(); - $this->assertEquals(14, $result['moves']); - $this->assertEquals('one', $result['goalBucket']); - $this->assertEquals(11, $result['otherBucket']); + $this->assertEquals(14, $result->moves); + $this->assertEquals('one', $result->goalBucket); + $this->assertEquals(11, $result->otherBucket); } /** @@ -72,9 +72,9 @@ public function testMeasureUsingBucketOneOfSize7AndBucketTwoOfSize11StartWithBuc $twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck); $result = $twoBucket->solve(); - $this->assertEquals(18, $result['moves']); - $this->assertEquals('two', $result['goalBucket']); - $this->assertEquals(7, $result['otherBucket']); + $this->assertEquals(18, $result->moves); + $this->assertEquals('two', $result->goalBucket); + $this->assertEquals(7, $result->otherBucket); } /** @@ -85,9 +85,9 @@ public function testMeasureOneStepUsingBucketOneOfSize1AndBucketTwoOfSize3StartW $twoBucket = new TwoBucket(1, 3, 3, 'two'); $result = $twoBucket->solve(); - $this->assertEquals(1, $result['moves']); - $this->assertEquals('two', $result['goalBucket']); - $this->assertEquals(0, $result['otherBucket']); + $this->assertEquals(1, $result->moves); + $this->assertEquals('two', $result->goalBucket); + $this->assertEquals(0, $result->otherBucket); } /** @@ -98,9 +98,9 @@ public function testMeasureUsingBucketOneOfSize2AndBucketTwoOfSize3StartWithBuck $twoBucket = new TwoBucket(2, 3, 3, 'one'); $result = $twoBucket->solve(); - $this->assertEquals(2, $result['moves']); - $this->assertEquals('two', $result['goalBucket']); - $this->assertEquals(2, $result['otherBucket']); + $this->assertEquals(2, $result->moves); + $this->assertEquals('two', $result->goalBucket); + $this->assertEquals(2, $result->otherBucket); } /** @@ -123,9 +123,9 @@ public function testReachabilityNotPossibleToReachGoalStartWithBucketOneAndEndWi $twoBucket = new TwoBucket(6, 15, 9, 'one'); $result = $twoBucket->solve(); - $this->assertEquals(10, $result['moves']); - $this->assertEquals('two', $result['goalBucket']); - $this->assertEquals(0, $result['otherBucket']); + $this->assertEquals(10, $result->moves); + $this->assertEquals('two', $result->goalBucket); + $this->assertEquals(0, $result->otherBucket); } /** @@ -140,9 +140,9 @@ public function testWithSameBucketsButDifferentGoalItIsPossible(): void $twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck); $result = $twoBucket->solve(); - $this->assertEquals(10, $result['moves']); - $this->assertEquals('two', $result['goalBucket']); - $this->assertEquals(0, $result['otherBucket']); + $this->assertEquals(10, $result->moves); + $this->assertEquals('two', $result->goalBucket); + $this->assertEquals(0, $result->otherBucket); } /** From 314d8a3fb0527dfdd2cb0fdab4ac6e47e369671f Mon Sep 17 00:00:00 2001 From: Tomas Norre Mikkelsen Date: Sat, 24 Feb 2024 13:36:53 +0100 Subject: [PATCH 4/6] Switch to Solution object --- .../practice/two-bucket/.meta/example.php | 55 ++++++++++++---- exercises/practice/two-bucket/TwoBucket.php | 12 +++- .../practice/two-bucket/TwoBucketTest.php | 64 +++++++++---------- 3 files changed, 86 insertions(+), 45 deletions(-) diff --git a/exercises/practice/two-bucket/.meta/example.php b/exercises/practice/two-bucket/.meta/example.php index b43b72d5..f039063c 100644 --- a/exercises/practice/two-bucket/.meta/example.php +++ b/exercises/practice/two-bucket/.meta/example.php @@ -1,14 +1,33 @@ . + * + * To disable strict typing, comment out the directive below. + */ + declare(strict_types=1); class TwoBucket { private int $goal; private array $buckets; - public int $moves; - public string $goalBucket; - public int $otherBucket; public function __construct(int $sizeBucketOne, int $sizeBucketTwo, int $goal, string $startBucket) { @@ -22,7 +41,7 @@ public function __construct(int $sizeBucketOne, int $sizeBucketTwo, int $goal, s $this->validate(); } - public function solve(): self + public function solve(): Solution { $this->first()->empty(); $this->second()->empty(); @@ -38,17 +57,19 @@ public function solve(): self while (true) { if ($this->first()->getAmount() === $this->goal) { - $this->moves = $moves; - $this->goalBucket = $this->first()->getName(); - $this->otherBucket = $this->second()->getAmount(); - return $this; + return new Solution( + $moves, + $this->first()->getName(), + $this->second()->getAmount() + ); } if ($this->second()->getAmount() === $this->goal) { - $this->moves = $moves; - $this->goalBucket = $this->second()->getName(); - $this->otherBucket = $this->first()->getAmount(); - return $this; + return new Solution( + $moves, + $this->second()->getName(), + $this->first()->getAmount() + ); } if ($this->first()->isEmpty()) { @@ -90,6 +111,16 @@ private function greatestCommonDivisor($a, $b) } } +class Solution +{ + public function __construct( + public int $numberOfActions, + public string $nameOfBucketWithDesiredLiters, + public int $litersLeftInOtherBucket, + ) { + } +} + class Bucket { private string $name; diff --git a/exercises/practice/two-bucket/TwoBucket.php b/exercises/practice/two-bucket/TwoBucket.php index 7ed53c5c..99d1dd09 100644 --- a/exercises/practice/two-bucket/TwoBucket.php +++ b/exercises/practice/two-bucket/TwoBucket.php @@ -31,8 +31,18 @@ public function __construct(int $sizeBucketOne, int $sizeBucketTwo, int $goal, s throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__)); } - public function solve(): self + public function solve(): Solution { throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__)); } } + +class Solution +{ + public function __construct( + public int $numberOfActions, + public string $nameOfBucketWithDesiredLiters, + public int $litersLeftInOtherBucket, + ) { + } +} diff --git a/exercises/practice/two-bucket/TwoBucketTest.php b/exercises/practice/two-bucket/TwoBucketTest.php index dd9fc91f..4651c1b5 100644 --- a/exercises/practice/two-bucket/TwoBucketTest.php +++ b/exercises/practice/two-bucket/TwoBucketTest.php @@ -19,11 +19,11 @@ public function testMeasureUsingBucketOneOfSize3AndBucketTwoOfSize5StartWithBuck $goal = 1; $starterBuck = 'one'; $twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck); - $result = $twoBucket->solve(); + $solution = $twoBucket->solve(); - $this->assertEquals(4, $result->moves); - $this->assertEquals('one', $result->goalBucket); - $this->assertEquals(5, $result->otherBucket); + $this->assertEquals(4, $solution->numberOfActions); + $this->assertEquals('one', $solution->nameOfBucketWithDesiredLiters); + $this->assertEquals(5, $solution->litersLeftInOtherBucket); } /** @@ -36,11 +36,11 @@ public function testMeasureUsingBucketOneOfSize3AndBucketTwoOfSize5StartWithBuck $goal = 1; $starterBuck = 'two'; $twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck); - $result = $twoBucket->solve(); + $solution = $twoBucket->solve(); - $this->assertEquals(8, $result->moves); - $this->assertEquals('two', $result->goalBucket); - $this->assertEquals(3, $result->otherBucket); + $this->assertEquals(8, $solution->numberOfActions); + $this->assertEquals('two', $solution->nameOfBucketWithDesiredLiters); + $this->assertEquals(3, $solution->litersLeftInOtherBucket); } /** @@ -53,11 +53,11 @@ public function testMeasureUsingBucketOneOfSize7AndBucketTwoOfSize11StartWithBuc $goal = 2; $starterBuck = 'one'; $twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck); - $result = $twoBucket->solve(); + $solution = $twoBucket->solve(); - $this->assertEquals(14, $result->moves); - $this->assertEquals('one', $result->goalBucket); - $this->assertEquals(11, $result->otherBucket); + $this->assertEquals(14, $solution->numberOfActions); + $this->assertEquals('one', $solution->nameOfBucketWithDesiredLiters); + $this->assertEquals(11, $solution->litersLeftInOtherBucket); } /** @@ -70,11 +70,11 @@ public function testMeasureUsingBucketOneOfSize7AndBucketTwoOfSize11StartWithBuc $goal = 2; $starterBuck = 'two'; $twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck); - $result = $twoBucket->solve(); + $solution = $twoBucket->solve(); - $this->assertEquals(18, $result->moves); - $this->assertEquals('two', $result->goalBucket); - $this->assertEquals(7, $result->otherBucket); + $this->assertEquals(18, $solution->numberOfActions); + $this->assertEquals('two', $solution->nameOfBucketWithDesiredLiters); + $this->assertEquals(7, $solution->litersLeftInOtherBucket); } /** @@ -83,11 +83,11 @@ public function testMeasureUsingBucketOneOfSize7AndBucketTwoOfSize11StartWithBuc public function testMeasureOneStepUsingBucketOneOfSize1AndBucketTwoOfSize3StartWithBucketTwo(): void { $twoBucket = new TwoBucket(1, 3, 3, 'two'); - $result = $twoBucket->solve(); + $solution = $twoBucket->solve(); - $this->assertEquals(1, $result->moves); - $this->assertEquals('two', $result->goalBucket); - $this->assertEquals(0, $result->otherBucket); + $this->assertEquals(1, $solution->numberOfActions); + $this->assertEquals('two', $solution->nameOfBucketWithDesiredLiters); + $this->assertEquals(0, $solution->litersLeftInOtherBucket); } /** @@ -96,11 +96,11 @@ public function testMeasureOneStepUsingBucketOneOfSize1AndBucketTwoOfSize3StartW public function testMeasureUsingBucketOneOfSize2AndBucketTwoOfSize3StartWithBucketOneAndEndWithBucketTwo(): void { $twoBucket = new TwoBucket(2, 3, 3, 'one'); - $result = $twoBucket->solve(); + $solution = $twoBucket->solve(); - $this->assertEquals(2, $result->moves); - $this->assertEquals('two', $result->goalBucket); - $this->assertEquals(2, $result->otherBucket); + $this->assertEquals(2, $solution->numberOfActions); + $this->assertEquals('two', $solution->nameOfBucketWithDesiredLiters); + $this->assertEquals(2, $solution->litersLeftInOtherBucket); } /** @@ -121,11 +121,11 @@ public function testReachabilityNotPossibleToReachGoalStartWithBucketOne(): void public function testReachabilityNotPossibleToReachGoalStartWithBucketOneAndEndWithBucketTwo(): void { $twoBucket = new TwoBucket(6, 15, 9, 'one'); - $result = $twoBucket->solve(); + $solution = $twoBucket->solve(); - $this->assertEquals(10, $result->moves); - $this->assertEquals('two', $result->goalBucket); - $this->assertEquals(0, $result->otherBucket); + $this->assertEquals(10, $solution->numberOfActions); + $this->assertEquals('two', $solution->nameOfBucketWithDesiredLiters); + $this->assertEquals(0, $solution->litersLeftInOtherBucket); } /** @@ -138,11 +138,11 @@ public function testWithSameBucketsButDifferentGoalItIsPossible(): void $goal = 9; $starterBuck = 'one'; $twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck); - $result = $twoBucket->solve(); + $solution = $twoBucket->solve(); - $this->assertEquals(10, $result->moves); - $this->assertEquals('two', $result->goalBucket); - $this->assertEquals(0, $result->otherBucket); + $this->assertEquals(10, $solution->numberOfActions); + $this->assertEquals('two', $solution->nameOfBucketWithDesiredLiters); + $this->assertEquals(0, $solution->litersLeftInOtherBucket); } /** From 72d39038dc68dd1ebbfb2ad7b617d8aaa7877839 Mon Sep 17 00:00:00 2001 From: Tomas Norre Mikkelsen Date: Sat, 24 Feb 2024 15:14:58 +0100 Subject: [PATCH 5/6] Remove return type from solve() --- exercises/practice/two-bucket/TwoBucket.php | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/exercises/practice/two-bucket/TwoBucket.php b/exercises/practice/two-bucket/TwoBucket.php index 99d1dd09..d8d2fc27 100644 --- a/exercises/practice/two-bucket/TwoBucket.php +++ b/exercises/practice/two-bucket/TwoBucket.php @@ -31,18 +31,8 @@ public function __construct(int $sizeBucketOne, int $sizeBucketTwo, int $goal, s throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__)); } - public function solve(): Solution + public function solve() { throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__)); } } - -class Solution -{ - public function __construct( - public int $numberOfActions, - public string $nameOfBucketWithDesiredLiters, - public int $litersLeftInOtherBucket, - ) { - } -} From 447ea83bf077e707fc3e34101b4530088ef5094c Mon Sep 17 00:00:00 2001 From: Tomas Norre Mikkelsen Date: Sun, 25 Feb 2024 18:20:20 +0100 Subject: [PATCH 6/6] Switch to only have solve() instead of __constructor() + solve() --- .../practice/two-bucket/.meta/example.php | 6 +-- exercises/practice/two-bucket/TwoBucket.php | 7 +--- .../practice/two-bucket/TwoBucketTest.php | 38 +++++++++---------- 3 files changed, 20 insertions(+), 31 deletions(-) diff --git a/exercises/practice/two-bucket/.meta/example.php b/exercises/practice/two-bucket/.meta/example.php index f039063c..b9ed88f0 100644 --- a/exercises/practice/two-bucket/.meta/example.php +++ b/exercises/practice/two-bucket/.meta/example.php @@ -29,7 +29,7 @@ class TwoBucket private int $goal; private array $buckets; - public function __construct(int $sizeBucketOne, int $sizeBucketTwo, int $goal, string $startBucket) + public function solve(int $sizeBucketOne, int $sizeBucketTwo, int $goal, string $startBucket): Solution { $this->goal = $goal; $this->buckets = [new Bucket('one', $sizeBucketOne), new Bucket('two', $sizeBucketTwo)]; @@ -39,10 +39,6 @@ public function __construct(int $sizeBucketOne, int $sizeBucketTwo, int $goal, s } $this->validate(); - } - - public function solve(): Solution - { $this->first()->empty(); $this->second()->empty(); $moves = 0; diff --git a/exercises/practice/two-bucket/TwoBucket.php b/exercises/practice/two-bucket/TwoBucket.php index d8d2fc27..7bd58607 100644 --- a/exercises/practice/two-bucket/TwoBucket.php +++ b/exercises/practice/two-bucket/TwoBucket.php @@ -26,12 +26,7 @@ class TwoBucket { - public function __construct(int $sizeBucketOne, int $sizeBucketTwo, int $goal, string $startBucket) - { - throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__)); - } - - public function solve() + public function solve(int $sizeBucketOne, int $sizeBucketTwo, int $goal, string $startBucket) { throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__)); } diff --git a/exercises/practice/two-bucket/TwoBucketTest.php b/exercises/practice/two-bucket/TwoBucketTest.php index 4651c1b5..63b2ea82 100644 --- a/exercises/practice/two-bucket/TwoBucketTest.php +++ b/exercises/practice/two-bucket/TwoBucketTest.php @@ -4,11 +4,18 @@ class TwoBucketTest extends PHPUnit\Framework\TestCase { + private TwoBucket $twoBucket; + public static function setUpBeforeClass(): void { require_once 'TwoBucket.php'; } + protected function setUp(): void + { + $this->twoBucket = new TwoBucket(); + } + /** * uuid: a6f2b4ba-065f-4dca-b6f0-e3eee51cb661 */ @@ -18,8 +25,7 @@ public function testMeasureUsingBucketOneOfSize3AndBucketTwoOfSize5StartWithBuck $buckTwo = 5; $goal = 1; $starterBuck = 'one'; - $twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck); - $solution = $twoBucket->solve(); + $solution = $this->twoBucket->solve($buckOne, $buckTwo, $goal, $starterBuck); $this->assertEquals(4, $solution->numberOfActions); $this->assertEquals('one', $solution->nameOfBucketWithDesiredLiters); @@ -35,8 +41,7 @@ public function testMeasureUsingBucketOneOfSize3AndBucketTwoOfSize5StartWithBuck $buckTwo = 5; $goal = 1; $starterBuck = 'two'; - $twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck); - $solution = $twoBucket->solve(); + $solution = $this->twoBucket->solve($buckOne, $buckTwo, $goal, $starterBuck); $this->assertEquals(8, $solution->numberOfActions); $this->assertEquals('two', $solution->nameOfBucketWithDesiredLiters); @@ -52,8 +57,7 @@ public function testMeasureUsingBucketOneOfSize7AndBucketTwoOfSize11StartWithBuc $buckTwo = 11; $goal = 2; $starterBuck = 'one'; - $twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck); - $solution = $twoBucket->solve(); + $solution = $this->twoBucket->solve($buckOne, $buckTwo, $goal, $starterBuck); $this->assertEquals(14, $solution->numberOfActions); $this->assertEquals('one', $solution->nameOfBucketWithDesiredLiters); @@ -69,8 +73,7 @@ public function testMeasureUsingBucketOneOfSize7AndBucketTwoOfSize11StartWithBuc $buckTwo = 11; $goal = 2; $starterBuck = 'two'; - $twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck); - $solution = $twoBucket->solve(); + $solution = $this->twoBucket->solve($buckOne, $buckTwo, $goal, $starterBuck); $this->assertEquals(18, $solution->numberOfActions); $this->assertEquals('two', $solution->nameOfBucketWithDesiredLiters); @@ -82,8 +85,7 @@ public function testMeasureUsingBucketOneOfSize7AndBucketTwoOfSize11StartWithBuc */ public function testMeasureOneStepUsingBucketOneOfSize1AndBucketTwoOfSize3StartWithBucketTwo(): void { - $twoBucket = new TwoBucket(1, 3, 3, 'two'); - $solution = $twoBucket->solve(); + $solution = $this->twoBucket->solve(1, 3, 3, 'two'); $this->assertEquals(1, $solution->numberOfActions); $this->assertEquals('two', $solution->nameOfBucketWithDesiredLiters); @@ -95,8 +97,7 @@ public function testMeasureOneStepUsingBucketOneOfSize1AndBucketTwoOfSize3StartW */ public function testMeasureUsingBucketOneOfSize2AndBucketTwoOfSize3StartWithBucketOneAndEndWithBucketTwo(): void { - $twoBucket = new TwoBucket(2, 3, 3, 'one'); - $solution = $twoBucket->solve(); + $solution = $this->twoBucket->solve(2, 3, 3, 'one'); $this->assertEquals(2, $solution->numberOfActions); $this->assertEquals('two', $solution->nameOfBucketWithDesiredLiters); @@ -111,8 +112,8 @@ public function testReachabilityNotPossibleToReachGoalStartWithBucketOne(): void $buckOne = 6; $buckTwo = 15; $this->expectException(Exception::class); - $twoBucket = new TwoBucket($buckOne, $buckTwo, 5, 'one'); - $twoBucket->solve(); + + $this->twoBucket->solve($buckOne, $buckTwo, 5, 'one'); } /** @@ -120,8 +121,7 @@ public function testReachabilityNotPossibleToReachGoalStartWithBucketOne(): void */ public function testReachabilityNotPossibleToReachGoalStartWithBucketOneAndEndWithBucketTwo(): void { - $twoBucket = new TwoBucket(6, 15, 9, 'one'); - $solution = $twoBucket->solve(); + $solution = $this->twoBucket->solve(6, 15, 9, 'one'); $this->assertEquals(10, $solution->numberOfActions); $this->assertEquals('two', $solution->nameOfBucketWithDesiredLiters); @@ -137,8 +137,7 @@ public function testWithSameBucketsButDifferentGoalItIsPossible(): void $buckTwo = 15; $goal = 9; $starterBuck = 'one'; - $twoBucket = new TwoBucket($buckOne, $buckTwo, $goal, $starterBuck); - $solution = $twoBucket->solve(); + $solution = $this->twoBucket->solve($buckOne, $buckTwo, $goal, $starterBuck); $this->assertEquals(10, $solution->numberOfActions); $this->assertEquals('two', $solution->nameOfBucketWithDesiredLiters); @@ -151,7 +150,6 @@ public function testWithSameBucketsButDifferentGoalItIsPossible(): void public function testGoalLargerThanBothBucketsIsImpossible(): void { $this->expectException(Exception::class); - $twoBucket = new TwoBucket(5, 7, 8, 'one'); - $twoBucket->solve(); + $this->twoBucket->solve(5, 7, 8, 'one'); } }