Skip to content

Commit

Permalink
'#15.'Optimize slicing algoritm, avoiding iterations thru finished st…
Browse files Browse the repository at this point in the history
…art positions
  • Loading branch information
seajer committed Feb 4, 2017
1 parent e5b0598 commit 9ae048f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
13 changes: 7 additions & 6 deletions src/main/java/com/google/hashcode/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand All @@ -30,18 +31,18 @@ public static void main(String[] args) throws IOException {

public static void slicePizza(String inputFile, String outputFile) throws IOException {
Profiler profiler = new Profiler();
List<Slice> output;
List<Slice> startPositions;
List<Slice> output = new ArrayList<>();
Pizza pizza = new Pizza(new File(inputFile), IoUtils.parsePizza(inputFile), IoUtils.parseSliceInstructions(inputFile));
//get start positions
output = DFSMethods.cutAllStartPositions(pizza);
startPositions = DFSMethods.cutAllStartPositions(pizza);
//get All steps
Map<Slice, List<Step>> availableSteps = DFSMethods.getAvailableSteps(pizza, output);
Map<Slice, List<Step>> availableSteps = DFSMethods.getAvailableSteps(pizza, startPositions, output);
while (!availableSteps.values().stream().allMatch(List::isEmpty)) {
Step step = DFSMethods.selectStep(availableSteps);
output.remove(step.startPosition);
output.add(DFSMethods.performStep(pizza, step));
DFSMethods.performStep(pizza, step, startPositions, output);
//TODO available steps should include merging slices to each other
availableSteps = DFSMethods.getAvailableSteps(pizza, output);
availableSteps = DFSMethods.getAvailableSteps(pizza, startPositions, output);
LOGGER.info("OUTPUT AFTER A STEP: "
+ "\n " + output);
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/google/hashcode/entity/Cell.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Cell)) return false;
Cell cell = (Cell) o;
return x == cell.x &&
y == cell.y &&
ingredient == cell.ingredient;
return this.x == cell.x &&
this.y == cell.y &&
this.ingredient == cell.ingredient;
}

@Override
Expand Down
28 changes: 21 additions & 7 deletions src/main/java/com/google/hashcode/utils/DFSMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ private DFSMethods() {
* remove this slice from startPositions and add all it's cells to pizza.
* @param pizza given pizza
* @param startPositions given slices in the pizza
* @param output
* @return available steps
*/
public static Map<Slice, List<Step>> getAvailableSteps(Pizza pizza, List<Slice> startPositions) {
public static Map<Slice, List<Step>> getAvailableSteps(Pizza pizza, List<Slice> startPositions, List<Slice> output) {
Map<Slice, List<Step>> groupedSteps = new HashMap<>();
Iterator iter = startPositions.iterator();
while (iter.hasNext()) {
Expand All @@ -43,7 +44,8 @@ public static Map<Slice, List<Step>> getAvailableSteps(Pizza pizza, List<Slice>
if (startPosition.isValid(pizza)) {
// if slice is valid and have'nt any steps -> cut it from
// startPositions
groupedSteps.put(startPosition, steps);
output.add(startPosition);
iter.remove();
} else {
// if slice isn't valid and have'nt any steps -> return all
// it cells to pizza
Expand All @@ -66,20 +68,32 @@ public static Map<Slice, List<Step>> getAvailableSteps(Pizza pizza, List<Slice>
*
* @param pizza given pizza
* @param step step to perform
* @return formed slice that includes an original slice and delta from a step
* @param startPositions
*@param output @return formed slice that includes an original slice and delta from a step
*/
public static Slice performStep(Pizza pizza, Step step) {
public static void performStep(Pizza pizza, Step step, List<Slice> startPositions, List<Slice> output) {
//1. Pick ups a steps list with minimal total cells number
LOGGER.info("STEP TO PERFORM " + step);
//2. Cut all the step delta cells from pizza
LOGGER.info("pizza before step: " + pizza
+ "\ndelta to remove from the pizza: " + step.delta);
pizza.getCells().removeAll(step.delta.cells);

//3. remove previous version start position from startPositions
startPositions.remove(step.startPosition);

List<Cell> returnedList = step.startPosition.cells;
returnedList.addAll(step.delta.cells);
Slice finalSlice = new Slice(returnedList);

LOGGER.info("PIZZA AFTER STEP:" + pizza);
//3. Add the step cells to an output slice
Slice slice = new Slice(step.delta.cells);
slice.cells.addAll(step.startPosition.cells);
return slice;

if(finalSlice.cells.size() == pizza.getSliceInstruction().getMaxNumberOfCellsPerSlice()){
output.add(finalSlice);
} else{
startPositions.add(finalSlice);
}
}

/**
Expand Down
14 changes: 9 additions & 5 deletions src/test/java/com/google/hashcode/utils/DFSMethodsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
Expand All @@ -27,7 +28,8 @@ public void setup() throws IOException {

@Test
public void getAvailableSteps() throws IOException {
Map<Slice, List<Step>> actualMap = DFSMethods.getAvailableSteps(pizza, DFSMethods.cutAllStartPositions(pizza));
List<Slice> output = new ArrayList<>();
Map<Slice, List<Step>> actualMap = DFSMethods.getAvailableSteps(pizza, DFSMethods.cutAllStartPositions(pizza), output);
assertEquals(3, actualMap.keySet().size());
assertEquals(3, actualMap.get(new Slice(new Cell(1, 1, Ingredient.MUSHROOM))).size());
assertEquals(2, actualMap.get(new Slice(new Cell(1, 2, Ingredient.MUSHROOM))).size());
Expand All @@ -47,10 +49,12 @@ public void cutAllStartPositions() throws IOException {

@Test
public void performStep() {
List<Slice> output = DFSMethods.cutAllStartPositions(pizza);
Map<Slice, List<Step>> availableSteps = DFSMethods.getAvailableSteps(pizza, output);
Slice slice = DFSMethods.performStep(pizza, DFSMethods.selectStep(availableSteps));
assertEquals(new Slice(Arrays.asList(new Cell(2, 2, Ingredient.TOMATO), new Cell(1, 2, Ingredient.MUSHROOM))), slice);
List<Slice> startPositions = DFSMethods.cutAllStartPositions(pizza);
List<Slice> output = new ArrayList<>();
Map<Slice, List<Step>> availableSteps = DFSMethods.getAvailableSteps(pizza, startPositions, output);
DFSMethods.performStep(pizza, DFSMethods.selectStep(availableSteps), startPositions, output);
assertEquals(new Slice(Arrays.asList(new Cell(1, 2, Ingredient.MUSHROOM), new Cell(2, 2, Ingredient.TOMATO)))
, startPositions.get(2));
assertEquals(11, pizza.getCells().size());
}
}

0 comments on commit 9ae048f

Please sign in to comment.