|
1 | | -type Bucket = 'one' | 'two' |
| 1 | +class Bucket { |
| 2 | + public name: string |
| 3 | + public readonly size: number |
| 4 | + public amount: number |
2 | 5 |
|
3 | | -export class TwoBucket { |
4 | | - private readonly starter: Bucket |
5 | | - private readonly x: number |
6 | | - private readonly y: number |
7 | | - private readonly z: number |
8 | | - |
9 | | - public goalBucket!: Bucket |
10 | | - public otherBucket!: number |
11 | | - |
12 | | - constructor(x: number, y: number, z: number, starter: Bucket) { |
13 | | - this.starter = starter |
14 | | - this.x = x |
15 | | - this.y = y |
16 | | - this.z = z |
| 6 | + constructor(name: string, size: number) { |
| 7 | + this.name = name |
| 8 | + this.size = size |
| 9 | + this.amount = 0 |
17 | 10 | } |
18 | 11 |
|
19 | | - private reachedGoal(measurements: number[]): boolean { |
20 | | - if (measurements[0] === this.z || measurements[1] === this.z) { |
21 | | - if (measurements[0] === this.z) { |
22 | | - this.goalBucket = 'one' |
23 | | - this.otherBucket = measurements[1] |
24 | | - } else { |
25 | | - this.goalBucket = 'two' |
26 | | - this.otherBucket = measurements[0] |
27 | | - } |
28 | | - return true |
29 | | - } |
30 | | - return false |
| 12 | + // accessors |
| 13 | + public get available(): number { |
| 14 | + return this.size - this.amount |
| 15 | + } |
| 16 | + public get isFull(): boolean { |
| 17 | + return this.amount === this.size |
| 18 | + } |
| 19 | + public get isEmpty(): boolean { |
| 20 | + return this.amount === 0 |
31 | 21 | } |
32 | 22 |
|
33 | | - private bigFirst( |
34 | | - measurements: number[], |
35 | | - moveCount: number, |
36 | | - prBool: boolean |
37 | | - ): number { |
38 | | - let j = measurements[0], |
39 | | - k = measurements[1] |
40 | | - while (!this.reachedGoal(measurements)) { |
41 | | - if (k > this.x && j === 0 && moveCount === 0) { |
42 | | - j = this.x |
43 | | - k = this.y - j |
44 | | - } else if (j === this.x) { |
45 | | - j = 0 |
46 | | - } else if ((k > this.x && j !== 0) || (k > this.x && prBool)) { |
47 | | - k -= this.x - j |
48 | | - j = this.x |
49 | | - } else if (k > this.x || j === 0) { |
50 | | - j = k |
51 | | - k -= j |
52 | | - } else if (k === 0) { |
53 | | - k = this.y |
54 | | - } |
55 | | - measurements = [j, k] |
56 | | - moveCount++ |
57 | | - prBool = !prBool |
| 23 | + public fill(): void { |
| 24 | + this.amount = this.size |
| 25 | + } |
| 26 | + public empty(): void { |
| 27 | + this.amount = 0 |
| 28 | + } |
| 29 | + |
| 30 | + public pourInto(other: Bucket): void { |
| 31 | + const quantity = Math.min(this.amount, other.available) |
| 32 | + this.amount -= quantity |
| 33 | + other.amount += quantity |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +const gcd: (a: number, b: number) => number = (a, b) => |
| 38 | + b === 0 ? a : gcd(b, a % b) |
| 39 | + |
| 40 | +export class TwoBucket { |
| 41 | + private readonly buckets: Bucket[] |
| 42 | + private readonly goal: number |
| 43 | + public goalBucket: string | undefined |
| 44 | + public otherBucket: number | undefined |
| 45 | + |
| 46 | + constructor(size1: number, size2: number, goal: number, start: string) { |
| 47 | + this.goal = goal |
| 48 | + this.buckets = [new Bucket('one', size1), new Bucket('two', size2)] |
| 49 | + |
| 50 | + if (start === 'two') { |
| 51 | + this.buckets.reverse() |
58 | 52 | } |
59 | | - return moveCount |
60 | 53 | } |
61 | 54 |
|
62 | | - private smallFirst( |
63 | | - measurements: number[], |
64 | | - moveCount: number, |
65 | | - prBool: boolean |
66 | | - ): number { |
67 | | - let j = measurements[0], |
68 | | - k = measurements[1] |
69 | | - while (!this.reachedGoal(measurements)) { |
70 | | - if (j === this.x && moveCount === 0) { |
71 | | - j = 0 |
72 | | - k = this.x |
73 | | - } else if (j === 0) { |
74 | | - j = this.x |
75 | | - } else if (j === this.x && k < this.y) { |
76 | | - const tempK = k |
77 | | - k + j > this.y ? (k = this.y) : (k = tempK + j) |
78 | | - tempK + j > this.y ? (j -= this.y - tempK) : (j = 0) |
79 | | - } else if (k === this.y) { |
80 | | - k = 0 |
81 | | - } else if (k === 0 && j < this.x) { |
82 | | - k = j |
83 | | - j = 0 |
84 | | - } |
85 | | - measurements = [j, k] |
86 | | - moveCount++ |
87 | | - prBool = !prBool |
| 55 | + private get first(): Bucket { |
| 56 | + return this.buckets[0] |
| 57 | + } |
| 58 | + private get second(): Bucket { |
| 59 | + return this.buckets[1] |
| 60 | + } |
| 61 | + |
| 62 | + private validate(): void { |
| 63 | + if (this.goal > Math.max(this.first.size, this.second.size)) { |
| 64 | + throw new Error('Goal is bigger than the largest bucket.') |
| 65 | + } |
| 66 | + |
| 67 | + if (this.goal % gcd(this.first.size, this.second.size) !== 0) { |
| 68 | + throw new Error( |
| 69 | + 'Goal must be a multiple of the GCD of the sizes of the two buckets.' |
| 70 | + ) |
88 | 71 | } |
89 | | - return moveCount |
90 | 72 | } |
91 | 73 |
|
92 | | - public moves(): number { |
93 | | - let j = 0 |
94 | | - let k = 0 // j will be running val of bucket one, k = running val of bucket two |
95 | | - this.starter === 'one' ? (j = this.x) : (k = this.y) |
96 | | - const measurements = [j, k] |
97 | | - let moveCount = 0 |
98 | | - const prBool = true // pour / receive boolean - need to pour or receive every other turn |
99 | | - if (this.starter === 'one') { |
100 | | - moveCount = this.smallFirst(measurements, moveCount, prBool) |
101 | | - } else { |
102 | | - moveCount = this.bigFirst(measurements, moveCount, prBool) |
| 74 | + private moves(): number { |
| 75 | + this.validate() |
| 76 | + |
| 77 | + this.first.empty() |
| 78 | + this.second.empty() |
| 79 | + let moves = 0 |
| 80 | + |
| 81 | + // fill the start bucket with the first move |
| 82 | + this.first.fill() |
| 83 | + moves += 1 |
| 84 | + |
| 85 | + // optimization: if the other bucket is the right size, |
| 86 | + // fill it immediately with the second move |
| 87 | + if (this.second.size === this.goal) { |
| 88 | + this.second.fill() |
| 89 | + moves += 1 |
| 90 | + } |
| 91 | + |
| 92 | + /* eslint-disable-next-line no-constant-condition */ |
| 93 | + while (true) { |
| 94 | + if (this.first.amount === this.goal) { |
| 95 | + this.goalBucket = this.first.name |
| 96 | + this.otherBucket = this.second.amount |
| 97 | + return moves |
| 98 | + } |
| 99 | + |
| 100 | + if (this.second.amount === this.goal) { |
| 101 | + this.goalBucket = this.second.name |
| 102 | + this.otherBucket = this.first.amount |
| 103 | + return moves |
| 104 | + } |
| 105 | + |
| 106 | + if (this.first.isEmpty) { |
| 107 | + this.first.fill() |
| 108 | + } else if (this.second.isFull) { |
| 109 | + this.second.empty() |
| 110 | + } else { |
| 111 | + this.first.pourInto(this.second) |
| 112 | + } |
| 113 | + |
| 114 | + moves += 1 |
103 | 115 | } |
104 | | - return moveCount + 1 // accounts for first move made before loop (and moveCount starts at zero before loop) |
105 | 116 | } |
106 | 117 | } |
0 commit comments