Skip to content

Commit

Permalink
[New Exercise]: Two Bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
glaxxie committed Oct 24, 2023
1 parent 52f8ee6 commit d512511
Show file tree
Hide file tree
Showing 7 changed files with 397 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,14 @@
"prerequisites": [],
"difficulty": 5
},
{
"slug": "two-bucket",
"name": "Two Bucket",
"uuid": "f16d5ba7-e15f-49e6-a391-e6a60c7171ff",
"practices": [],
"prerequisites": [],
"difficulty": 6
},
{
"slug": "forth",
"name": "Forth",
Expand Down
46 changes: 46 additions & 0 deletions exercises/practice/two-bucket/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -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/
172 changes: 172 additions & 0 deletions exercises/practice/two-bucket/.meta/TwoBucket.example.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<#
.SYNOPSIS
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.
.DESCRIPTION
Please read the rules of how to implement the solution in instructions.
Your task here is to implement a class to solve for the solution.
The class should take in : size of bucket 1, size of bucket 2, and which bucket to start with "one" or "two"
The class should have the Measure method that take in the target, and if possible return an object for the result.
If it is not possible to reach the target, please throw an error.
.EXAMPLE
$buckets = [TwoBucket]::new(6, 7, "one")
$buckets.Measure(5) | Format-List
Returns:
Moves : 4
GoalBucket : one
OtherBucket : 7
#>
Class State {
<#
.DESCRIPTION
A state class, represent amount of water in each bucket, and how many moves have been made to get to this state.
#>
[int] hidden $water1
[int] hidden $water2
[int] hidden $move
[int] static hidden $PRIME1 = 17
[int] static hidden $PRIME2 = 53

State([int] $bucket1, [int] $bucket2, [int] $move) {
$this.water1 = $bucket1
$this.water2 = $bucket2
$this.move = $move
}

[bool] TargetReached([int] $target) {
return $this.water1 -eq $target -or $this.water2 -eq $target
}

[PSCustomObject] GetResult([int]$target) {
return [PSCustomObject]@{
Moves = $this.move
GoalBucket = $this.water1 -eq $target ? "one" : "two"
OtherBucket = $this.water1 -eq $target ? $this.water2 : $this.water1
}
}
#these 2 need to be implemeted so State can work nicely in a hashset
[bool] hidden Equals($other) {
$equalBucket1 = $this.water1 -eq $other.water1
$equalBucket2 = $this.water2 -eq $other.water2
return $equalBucket1 -and $equalBucket2
}

[int] hidden GetHashCode() {
return ([State]::PRIME1 * $this.water1) + ([State]::PRIME2 * $this.water2)
}
}

Class TwoBucket {
[int] $sizeOne
[int] $sizeTwo
[int] hidden $FIRST
[int] hidden $SECOND
[int] hidden $start
[State] hidden $setupState

TwoBucket([int] $size1, [int] $size2, [string] $start) {
$this.FIRST = 1
$this.SECOND = -1
$this.sizeOne = $size1
$this.sizeTwo = $size2
$this.start = $start -eq "one" ? $this.FIRST : $this.SECOND
$this.setupState = [State]::new(0, 0, 0)
}

[PSCustomObject] Measure([int] $target) {
<#
.DESCRIPTION
Main method
#>
$this.ValidateBuckets($this.sizeOne, $this.sizeTwo, $target)

$initialStart = $this.Fill($this.setupState, $this.start)
$unallowedState = $this.Fill($this.setupState, -$this.start)

$invalidStates = [System.Collections.Generic.HashSet[State]]::new()
$invalidStates.Add($unallowedState)

$queue = [System.Collections.Queue]::new(@($initialStart))

while ($queue.Count) {
$currentState = $queue.Dequeue()

if ($currentState.TargetReached($target)) {
return $currentState.GetResult($target)
}

$invalidStates.Add($currentState)
$actions = $this.GetActions($currentState)

foreach ($action in $actions) {
if (-not $invalidStates.Contains($action)) {
$queue.Enqueue($action)
}
}
}
return $null
}

[State[]] hidden GetActions([State] $state) {
<#
.DESCRIPTION
Method to perform all possible actions from a state
#>
$fill1 = $this.Fill($state,$this.FIRST)
$fill2 = $this.Fill($state,$this.SECOND)
$empty1 = $this.Empty($state,$this.FIRST)
$empty2 = $this.Empty($state,$this.SECOND)
$pour1to2 = $this.Pour($state,$this.FIRST,$this.SECOND)
$pour2to1 = $this.Pour($state,$this.SECOND,$this.FIRST)
return $fill1, $fill2, $empty1, $empty2, $pour1to2, $pour2to1
}

[State] hidden Fill([State] $state, [int] $bucket) {
$water1 = $bucket -eq $this.FIRST ? $this.sizeOne : $state.water1
$water2 = $bucket -eq $this.SECOND ? $this.sizeTwo : $state.water2
$move = $state.move + 1
return [State]::new($water1, $water2, $move)
}

[State] hidden Empty([State] $state, [int] $bucket) {
$water1 = $bucket -eq $this.FIRST ? 0 : $state.water1
$water2 = $bucket -eq $this.SECOND ? 0 : $state.water2
$move = $state.move + 1
return [State]::new($water1, $water2, $move)
}

[State] hidden Pour([State] $state, [int] $from, [int] $to) {
if ($from -eq $this.FIRST) {
$give = [math]::Min($state.water1, $this.sizeTwo - $state.water2)
$water1 = $state.water1 - $give
$water2 = $state.water2 + $give
}else {
$give = [math]::Min($state.water2, $this.sizeOne - $state.water1)
$water1 = $state.water1 + $give
$water2 = $state.water2 - $give
}
$move = $state.move + 1
return [State]::new($water1, $water2, $move)
}

[void] hidden ValidateBuckets([int] $bucket1, [int] $bucket2, [int] $target) {
<#
.DESCRIPTION
Helper function to validate buckets and determine if a goal is possible
#>
if ($bucket1 -eq $bucket2) {
Throw "Two buckets can't be of the same size"
}
if ($target -gt $bucket1 -and $target -gt $bucket2) {
Throw "Target is impossible to reach"
}
$gcd = [System.Numerics.BigInteger]::GreatestCommonDivisor($bucket1, $bucket2)
if ($target % $gcd) {
Throw "Target is impossible to reach"
}
}
}
19 changes: 19 additions & 0 deletions exercises/practice/two-bucket/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"glaxxie"
],
"files": {
"solution": [
"TwoBucket.ps1"
],
"test": [
"TwoBucket.tests.ps1"
],
"example": [
".meta/TwoBucket.example.ps1"
]
},
"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/"
}
37 changes: 37 additions & 0 deletions exercises/practice/two-bucket/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -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"
32 changes: 32 additions & 0 deletions exercises/practice/two-bucket/TwoBucket.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<#
.SYNOPSIS
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.
.DESCRIPTION
Please read the rules of how to implement the solution in instructions.
Your task here is to implement a class to solve for the solution.
The class should take in : size of bucket 1, size of bucket 2, and which bucket to start with "one" or "two"
The class should have the Measure method that take in the target, and if possible return an object for the result.
If it is not possible to reach the target, please throw an error.
.EXAMPLE
$buckets = [TwoBucket]::new(6, 7, "one")
$buckets.Measure(5) | Format-List
Returns:
Moves : 4
GoalBucket : one
OtherBucket : 7
#>

Class TwoBucket {
TwoBucket([int] $size1, [int] $size2, [string] $start) {
Throw "Please implement this class"
}

[object] Measure([int] $target) {
Throw "Please implement this function"
}
}
Loading

0 comments on commit d512511

Please sign in to comment.