|
| 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 | +} |
0 commit comments