Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
randeepbydesign committed Sep 7, 2018
1 parent 3b3680f commit 8fe691e
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -21,3 +21,6 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

.idea/
class/
42 changes: 42 additions & 0 deletions src/main/java/org/waterpouring/Action.java
@@ -0,0 +1,42 @@
package org.waterpouring;

import java.util.Arrays;

/**
* Defines a single action that can be taken to proceed in solving a water-pouring problem.
* One or more actions will combine to create the total solution if it can be solved.
*/
public class Action {
Integer[] glassIndex;
MoveType moveType;
Integer[] endState;

/**
* Create an action
* @param JUGS The jugs, in gallon size, that we are working with
* @param startState The state at the time of taking this action
* @param moveType
* @param jugIndex The indices of the jugs we are working with. Depending on the move type this can
* be 1 or 2 values
* @return
*/
public static Action of(Integer[] JUGS, Integer[] startState, MoveType moveType, Integer... jugIndex) {
return new Action(JUGS, startState, moveType, jugIndex);
}

private Action(Integer[] GLASSES, Integer[] startState, MoveType moveType, Integer... glassIndex) {
this.glassIndex = glassIndex;
this.moveType = moveType;
this.endState = moveType==null ?
startState : moveType.move.doMove(GLASSES, startState, glassIndex);
}

@Override
public String toString() {
return "Action{" +
"glassIndex=" + Arrays.toString(glassIndex) +
", moveType=" + moveType +
", endState=" + Arrays.toString(endState) +
'}';
}
}
16 changes: 16 additions & 0 deletions src/main/java/org/waterpouring/Move.java
@@ -0,0 +1,16 @@
package org.waterpouring;

/**
* A move describes some action taken on one or more jugs
*/
@FunctionalInterface
public interface Move {
/**
*
* @param JUGS the jugs, defined by their gallon capacity, for the problem
* @param state The current capacity of each jug
* @param jugIndex The indices of the various jugs to be operated on
* @return the new state of the jugs after performing the given move
*/
Integer[] doMove(final Integer[] JUGS, Integer[] state, Integer[] jugIndex);
}
34 changes: 34 additions & 0 deletions src/main/java/org/waterpouring/MoveType.java
@@ -0,0 +1,34 @@
package org.waterpouring;


/**
* An enumeration of all possible moves
*/
public enum MoveType {
/** Dump a jug out so that it is empty. Receives a single index parameter in its associated move */
EMPTY(new Move() {
@Override
public Integer[] doMove(Integer[] JUGS, Integer[] state, Integer[] jugIndex) {
return new Integer[0];
}
}), //TODO:
/** Pour water into a jug up so that it is full. Receives a single index parameter in its move */
FILL(new Move() {
@Override
public Integer[] doMove(Integer[] JUGS, Integer[] state, Integer[] jugIndex) {
return new Integer[0];
}
}), //TODO:
/** Pour the contents from one jug to another. Receives two index parameters: 0 = the from jug, 1 = the to jug */
POUR(new Move() {
@Override
public Integer[] doMove(Integer[] JUGS, Integer[] state, Integer[] jugIndex) {
return new Integer[0];
}
}); //TODO:

public final Move move;
MoveType(Move move) {
this.move = move;
}
}
84 changes: 84 additions & 0 deletions src/main/java/org/waterpouring/Solver.java
@@ -0,0 +1,84 @@
package org.waterpouring;

import java.util.*;

public class Solver {
final Integer[] JUGS;
final Integer GOAL;

// The initial state we begin with (all jugs empty)
final Integer[] startState;

/**
*
* @param jugs The capacity, in gallons, of the jugs we are working with
* @param goal in gallons, of the desired amount
*/
public Solver(final Integer[] jugs, final Integer goal) {
this.JUGS = jugs;
this.GOAL = goal;
this.startState = new Integer[]{JUGS.length};
for(int i=0; i<startState.length; i++) {
this.startState[i] = 0;
}
}

/**
* For the given state, return all possible actions that can be taken
* @param state
* @return
*/
public List<Action> createActions(Integer[] state) {
//TODO:
return null;
}

/**
* Look within the tree of all possible actions to see if a goal solution exists
* @param actions
* @return
*/
public boolean solutionExists(List<List<Action>> actions) {
//TODO:
return false;
}

/**
* For the given trail, take all the possible actions in the next list and create
* a new list for each one where the trail of actions becomes the head of the list,
* and the next element is appended to the end.
* @param trail of actions
* @param next possible next actions from the end of the trail.
* @return For example: <p>
* trail = "A", "B", "C" and next = "D", "E", "F"
* <p>yields:
* <ul>
* <li> "A", "B", "C", "D"</li>
* <li> "A", "B", "C", "E"</li>
* <li> "A", "B", "C", "F"</li>
* </ul>
*/
public List<List<Action>> combineTrails(List<Action> trail, List<Action> next) {
//TODO:
return null;
}

/**
* For the given parameters, find a solution where the Glass capacities can produce
* the goal amount.
* @return A list of actions that yield the goal amount, or null if no solution exists
*/
public List<Action> findSolution() {
//TODO:
return null;
}

public static void main(String[] args) {
List<Action> solution = new Solver(new Integer[]{5, 3}, 4).findSolution();
if(solution != null) {
System.out.println("Solution found: " + solution);
} else {
System.out.println("No solution found for the given parameters");
}
}
}

0 comments on commit 8fe691e

Please sign in to comment.