Skip to content

Commit b18be2a

Browse files
committed
DFSmethods. Finish a scratch for example pizza slicing
1 parent 3a4dc59 commit b18be2a

File tree

5 files changed

+59
-37
lines changed

5 files changed

+59
-37
lines changed

outputDataSet/example.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
2
2-
0 0 1 1
3-
0 2 1 3
1+
3
2+
0 2 2 2
3+
0 3 2 4
4+
0 0 2 1

src/main/java/com/google/hashcode/App.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.google.hashcode.entity.Pizza;
44
import com.google.hashcode.entity.Slice;
55
import com.google.hashcode.entity.Step;
6-
import com.google.hashcode.service.Slicer;
76
import com.google.hashcode.utils.DFSMethods;
87
import com.google.hashcode.utils.IoUtils;
98
import org.slf4j.Logger;
@@ -18,27 +17,30 @@
1817

1918

2019
public class App {
21-
private static final Logger LOGGER = LoggerFactory.getLogger(Slicer.class);
20+
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
2221

2322
public static void main(String[] args) throws IOException {
23+
slicePizza(EXAMPLE_INPUT_FILE_PATH, "outputDataSet/example.txt");
24+
25+
}
26+
27+
public static void slicePizza(String exampleInputFilePath, String outputFile) throws IOException {
2428
List<Slice> output;
25-
Pizza pizza = new Pizza(new File(EXAMPLE_INPUT_FILE_PATH), IoUtils.parsePizza(EXAMPLE_INPUT_FILE_PATH), IoUtils.parseSliceInstructions(EXAMPLE_INPUT_FILE_PATH));
29+
Pizza pizza = new Pizza(new File(exampleInputFilePath), IoUtils.parsePizza(exampleInputFilePath), IoUtils.parseSliceInstructions(exampleInputFilePath));
2630
//get start positions
2731
output = DFSMethods.cutAllStartPositions(pizza);
2832
//get All steps
2933
Map<Slice, List<Step>> availableSteps = DFSMethods.getAvailableSteps(pizza, output);
30-
while (availableSteps.size() > 0) {
34+
while (!availableSteps.values().stream().allMatch(List::isEmpty)) {
3135
Step step = DFSMethods.selectStep(availableSteps);
3236
output.remove(step.startPosition);
3337
output.add(DFSMethods.performStep(pizza, step));
3438
availableSteps = DFSMethods.getAvailableSteps(pizza, output);
3539
LOGGER.info("OUTPUT AFTER A STEP: "
36-
+"\n " + output);
40+
+ "\n " + output);
3741
}
38-
IoUtils.writeToFile("outputDataSet/example.txt", IoUtils.parseSlices(output));
39-
LOGGER.info("GoogleHashCode2017! Pizza task");
40-
LOGGER.info(pizza.toString());
41-
42+
IoUtils.writeToFile(outputFile, IoUtils.parseSlices(output));
43+
LOGGER.info("FINISHED for " + exampleInputFilePath + "!!!!!");
4244
}
4345

4446
}

src/main/java/com/google/hashcode/entity/Pizza.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,32 @@ public boolean containsCells(Slice slice) {
6363
}
6464

6565
private String outputCellsArray() {
66-
StringBuilder stringBuilder = new StringBuilder();
67-
int columnsCount = cells.stream().max(Comparator.comparingInt(Cell::getX)).get().getX();
68-
int rowsCount = cells.stream().max(Comparator.comparingInt(Cell::getY)).get().getY();
69-
//output columns coordinates
70-
stringBuilder.append(" ");
71-
for (int column = 0; column < columnsCount + 1; column++) {
72-
stringBuilder.append(" ").append(column);
73-
}
74-
stringBuilder.append("\n");
75-
for (int row = 0; row < rowsCount + 1; row++) {
76-
//output rows coordinates
77-
stringBuilder.append(row).append(" ");
66+
if (!cells.isEmpty()) {
67+
StringBuilder stringBuilder = new StringBuilder();
68+
int columnsCount = cells.stream().max(Comparator.comparingInt(Cell::getX)).get().getX();
69+
int rowsCount = cells.stream().max(Comparator.comparingInt(Cell::getY)).get().getY();
70+
//output columns coordinates
71+
stringBuilder.append(" ");
7872
for (int column = 0; column < columnsCount + 1; column++) {
79-
if (this.getCell(row, column).isPresent()) {
80-
stringBuilder.append(this.getCell(row, column).get().toString()).append(" ");
81-
} else {
82-
stringBuilder.append(" ").append(" ");
83-
}
73+
stringBuilder.append(" ").append(column);
8474
}
8575
stringBuilder.append("\n");
76+
for (int row = 0; row < rowsCount + 1; row++) {
77+
//output rows coordinates
78+
stringBuilder.append(row).append(" ");
79+
for (int column = 0; column < columnsCount + 1; column++) {
80+
if (this.getCell(row, column).isPresent()) {
81+
stringBuilder.append(this.getCell(row, column).get().toString()).append(" ");
82+
} else {
83+
stringBuilder.append(" ").append(" ");
84+
}
85+
}
86+
stringBuilder.append("\n");
87+
}
88+
return stringBuilder.toString();
89+
} else {
90+
return "";
8691
}
87-
return stringBuilder.toString();
8892
}
8993

9094

src/main/java/com/google/hashcode/entity/Slice.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ public int minX() {
4545
}
4646

4747
public int minY() {
48-
return Collections.min(cells, Comparator.comparingInt(Cell::getX)).y;
48+
return Collections.min(cells, Comparator.comparingInt(Cell::getY)).y;
4949
}
5050

5151
public int maxX() {
5252
return Collections.max(cells, Comparator.comparingInt(Cell::getX)).x;
5353
}
5454

5555
public int maxY() {
56-
return Collections.max(cells, Comparator.comparingInt(Cell::getX)).y;
56+
return Collections.max(cells, Comparator.comparingInt(Cell::getY)).y;
5757
}
5858

5959

@@ -200,7 +200,7 @@ public Step generateStepRight(Pizza pizza) {
200200
if (cell.isPresent()) {
201201
delta.cells.add(cell.get());
202202
} else {
203-
LOGGER.info("cant perform step left !");
203+
LOGGER.info("cant perform step right !");
204204
return null;
205205
}
206206
}

src/main/java/com/google/hashcode/utils/DFSMethods.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,26 @@ public static Slice performStep(Pizza pizza, Step step) {
6565
return slice;
6666
}
6767

68+
/**
69+
* Selects a step which start position has minimal delta in all the steps
70+
*
71+
* @param steps
72+
* @return
73+
*/
6874
public static Step selectStep(Map<Slice, List<Step>> steps) {
69-
Step step = steps.values().stream()
70-
.min((o1, o2) -> new StepsComparator().compare(o1, o2)).get().get(0);
71-
LOGGER.info("step with minimal number of delta cells: " + step);
72-
return step;
75+
List<Step> min = steps.values().stream()
76+
.min(Comparator.comparingLong(value -> value.stream().map(step -> step.delta.cells.size()).count())).get();
77+
if (!min.isEmpty()) {
78+
LOGGER.info("steps list with minimal number of delta cells: " + min);
79+
return min.get(0);
80+
} else {
81+
Optional<List<Step>> optionalStep = steps.values().stream().filter(steps1 -> !steps1.isEmpty()).findFirst();
82+
if (optionalStep.isPresent()) {
83+
final Step step = optionalStep.get().get(0);
84+
LOGGER.info("Selected step to perform:" + step);
85+
return step;
86+
} else return null;
87+
}
7388
}
7489

7590
/**

0 commit comments

Comments
 (0)