From 9ae048f8e551432f3d2c9527fd31164ceec32931 Mon Sep 17 00:00:00 2001 From: seajer Date: Sat, 4 Feb 2017 18:05:19 +0200 Subject: [PATCH] '#15.'Optimize slicing algoritm, avoiding iterations thru finished start positions --- src/main/java/com/google/hashcode/App.java | 13 +++++---- .../java/com/google/hashcode/entity/Cell.java | 6 ++-- .../com/google/hashcode/utils/DFSMethods.java | 28 ++++++++++++++----- .../google/hashcode/utils/DFSMethodsTest.java | 14 ++++++---- 4 files changed, 40 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/google/hashcode/App.java b/src/main/java/com/google/hashcode/App.java index 26455da..f29fe42 100644 --- a/src/main/java/com/google/hashcode/App.java +++ b/src/main/java/com/google/hashcode/App.java @@ -11,6 +11,7 @@ import java.io.File; import java.io.IOException; +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -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 output; + List startPositions; + List 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> availableSteps = DFSMethods.getAvailableSteps(pizza, output); + Map> 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); } diff --git a/src/main/java/com/google/hashcode/entity/Cell.java b/src/main/java/com/google/hashcode/entity/Cell.java index 9993a15..7544720 100644 --- a/src/main/java/com/google/hashcode/entity/Cell.java +++ b/src/main/java/com/google/hashcode/entity/Cell.java @@ -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 diff --git a/src/main/java/com/google/hashcode/utils/DFSMethods.java b/src/main/java/com/google/hashcode/utils/DFSMethods.java index 18534d2..f76336f 100644 --- a/src/main/java/com/google/hashcode/utils/DFSMethods.java +++ b/src/main/java/com/google/hashcode/utils/DFSMethods.java @@ -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> getAvailableSteps(Pizza pizza, List startPositions) { + public static Map> getAvailableSteps(Pizza pizza, List startPositions, List output) { Map> groupedSteps = new HashMap<>(); Iterator iter = startPositions.iterator(); while (iter.hasNext()) { @@ -43,7 +44,8 @@ public static Map> getAvailableSteps(Pizza pizza, List 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 @@ -66,20 +68,32 @@ public static Map> getAvailableSteps(Pizza pizza, List * * @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 startPositions, List 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 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); + } } /** diff --git a/src/test/java/com/google/hashcode/utils/DFSMethodsTest.java b/src/test/java/com/google/hashcode/utils/DFSMethodsTest.java index 58578e6..fa95533 100644 --- a/src/test/java/com/google/hashcode/utils/DFSMethodsTest.java +++ b/src/test/java/com/google/hashcode/utils/DFSMethodsTest.java @@ -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; @@ -27,7 +28,8 @@ public void setup() throws IOException { @Test public void getAvailableSteps() throws IOException { - Map> actualMap = DFSMethods.getAvailableSteps(pizza, DFSMethods.cutAllStartPositions(pizza)); + List output = new ArrayList<>(); + Map> 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()); @@ -47,10 +49,12 @@ public void cutAllStartPositions() throws IOException { @Test public void performStep() { - List output = DFSMethods.cutAllStartPositions(pizza); - Map> 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 startPositions = DFSMethods.cutAllStartPositions(pizza); + List output = new ArrayList<>(); + Map> 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()); } } \ No newline at end of file