Skip to content

Commit 3a4dc59

Browse files
committed
DFSmethods. BugFixing
1 parent 45d1806 commit 3a4dc59

File tree

11 files changed

+233
-151
lines changed

11 files changed

+233
-151
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ public static void main(String[] args) throws IOException {
3232
output.remove(step.startPosition);
3333
output.add(DFSMethods.performStep(pizza, step));
3434
availableSteps = DFSMethods.getAvailableSteps(pizza, output);
35+
LOGGER.info("OUTPUT AFTER A STEP: "
36+
+"\n " + output);
3537
}
36-
//IoUtils.writeToFile("outputDataSet/example.txt", IoUtils.parseSlices());
38+
IoUtils.writeToFile("outputDataSet/example.txt", IoUtils.parseSlices(output));
3739
LOGGER.info("GoogleHashCode2017! Pizza task");
3840
LOGGER.info(pizza.toString());
3941

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

Lines changed: 112 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,49 @@ public int maxY() {
5656
return Collections.max(cells, Comparator.comparingInt(Cell::getX)).y;
5757
}
5858

59+
60+
/**
61+
* Coordinates are like in a 2D array
62+
*
63+
* @param y - row number, 0..max row number
64+
* @param x - column number,0..max column number
65+
* @return a pizza cell with specified coordinated
66+
*/
67+
public Optional<Cell> getCell(int y, int x) {
68+
return cells.stream().filter(cell -> cell.x == x && cell.y == y).findFirst();
69+
}
70+
5971
@Override
6072
public String toString() {
61-
return cells.toString();
73+
StringBuilder stringBuilder = new StringBuilder();
74+
stringBuilder.append("slice : \n");
75+
//output coordinates
76+
int columnsCount = cells.stream().max(Comparator.comparingInt(Cell::getX)).get().getX();
77+
int rowsCount = cells.stream().max(Comparator.comparingInt(Cell::getY)).get().getY();
78+
//output columns coordinates
79+
stringBuilder.append(" ");
80+
for (int column = 0; column < columnsCount + 1; column++) {
81+
stringBuilder.append(" ").append(column);
82+
}
83+
stringBuilder.append("\n");
84+
for (int row = 0; row < rowsCount + 1; row++) {
85+
//output rows coordinates
86+
stringBuilder.append(row).append(" ");
87+
for (int column = 0; column < columnsCount + 1; column++) {
88+
if (this.getCell(row, column).isPresent()) {
89+
stringBuilder.append(this.getCell(row, column).get().toString()).append(" ");
90+
} else {
91+
stringBuilder.append(" ").append(" ");
92+
}
93+
}
94+
stringBuilder.append("\n");
95+
}
96+
return stringBuilder.toString().trim();
6297
}
6398

64-
6599
/**
66100
* check if slice valid for current pizza.
101+
*
67102
* @param pizza
68103
* @return
69104
*/
@@ -88,53 +123,96 @@ public boolean isValid(Pizza pizza) {
88123

89124
//region generate steps
90125

91-
92-
public Slice generateStepDeltaAbove() {
93-
List<Cell> delta = new ArrayList<>();
126+
public Step generateStepAbove(Pizza pizza) {
127+
Slice delta = new Slice();
94128
for (int x = this.minX(); x <= this.maxX(); x++) {
95-
Cell cell = new Cell(this.minY() - 1, x, Ingredient.TOMATO);
96-
delta.add(cell);
129+
//try to get a cell
130+
Optional<Cell> cell = pizza.getCell(this.minY() - 1, x);
131+
if (cell.isPresent()) {
132+
delta.cells.add(cell.get());
133+
} else {
134+
LOGGER.info("cant perform step left !");
135+
return null;
136+
}
137+
}
138+
LOGGER.debug("generateStepLeft"
139+
+ "\nstep left delta: " + delta.toString());
140+
Step step = new Step(this, delta);
141+
if (step.isValid(pizza)) {
142+
return step;
143+
} else {
144+
LOGGER.info("step is invalid !");
145+
return null;
97146
}
98-
LOGGER.info("generateStepDeltaAbove"
99-
+ "\nslice :" + this.toString()
100-
+ "\nstep above delta: " + delta.toString());
101-
return new Slice(delta);
102147
}
103148

104-
public Slice generateStepDeltaBelow() {
105-
List<Cell> delta = new ArrayList<>();
149+
public Step generateStepBelow(Pizza pizza) {
150+
Slice delta = new Slice();
106151
for (int x = this.minX(); x <= this.maxX(); x++) {
107-
Cell cell = new Cell(this.maxY() + 1, x, Ingredient.TOMATO);
108-
delta.add(cell);
152+
//try to get a cell
153+
Optional<Cell> cell = pizza.getCell(this.maxY() + 1, x);
154+
if (cell.isPresent()) {
155+
delta.cells.add(cell.get());
156+
} else {
157+
LOGGER.info("cant perform step left !");
158+
return null;
159+
}
160+
}
161+
LOGGER.debug("generateStepLeft"
162+
+ "\nstep left delta: " + delta.toString());
163+
Step step = new Step(this, delta);
164+
if (step.isValid(pizza)) {
165+
return step;
166+
} else {
167+
LOGGER.info("step is invalid !");
168+
return null;
109169
}
110-
LOGGER.info("generateStepDeltaBelow"
111-
+ "\nslice :" + this.toString()
112-
+ "\nstep below delta: " + delta.toString());
113-
return new Slice(delta);
114170
}
115171

116-
public Slice generateStepDeltaLeft() {
117-
List<Cell> delta = new ArrayList<>();
172+
public Step generateStepLeft(Pizza pizza) {
173+
Slice delta = new Slice();
118174
for (int y = this.minY(); y <= this.maxY(); y++) {
119-
Cell cell = new Cell(y, minX() - 1, Ingredient.TOMATO);
120-
delta.add(cell);
175+
//try to get a cell
176+
Optional<Cell> cell = pizza.getCell(y, minX() - 1);
177+
if (cell.isPresent()) {
178+
delta.cells.add(cell.get());
179+
} else {
180+
LOGGER.info("cant perform step left !");
181+
return null;
182+
}
121183
}
122-
LOGGER.info("generateStepDeltaLeft"
123-
+ "\nslice :" + this.toString()
184+
LOGGER.debug("generateStepLeft"
124185
+ "\nstep left delta: " + delta.toString());
125-
return new Slice(delta);
186+
Step step = new Step(this, delta);
187+
if (step.isValid(pizza)) {
188+
return step;
189+
} else {
190+
LOGGER.info("step is invalid !");
191+
return null;
192+
}
126193
}
127194

128-
public Slice generateStepDeltaRight() {
129-
List<Cell> delta = new ArrayList<>();
195+
public Step generateStepRight(Pizza pizza) {
196+
Slice delta = new Slice();
130197
for (int y = this.minY(); y <= this.maxY(); y++) {
131-
Cell cell = new Cell(y, maxX() + 1, Ingredient.TOMATO);
132-
delta.add(cell);
198+
//try to get a cell
199+
Optional<Cell> cell = pizza.getCell(y, maxX()+1);
200+
if (cell.isPresent()) {
201+
delta.cells.add(cell.get());
202+
} else {
203+
LOGGER.info("cant perform step left !");
204+
return null;
205+
}
206+
}
207+
LOGGER.debug("generateStepLeft"
208+
+ "\nstep left delta: " + delta.toString());
209+
Step step = new Step(this, delta);
210+
if (step.isValid(pizza)) {
211+
return step;
212+
} else {
213+
LOGGER.info("step is invalid !");
214+
return null;
133215
}
134-
LOGGER.info("generateStepDeltaRight"
135-
+ "\nslice :" + this.toString()
136-
+ "\nstep right delta: " + delta.toString());
137-
return new Slice(delta);
138216
}
139217
//endregion
140218

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.google.hashcode.entity;
22

3-
import java.util.List;
3+
import java.util.ArrayList;
44

55
/**
66
* Step as an entity is a slice tha can be added to a particular slice inside a particular pizza, considering
@@ -19,21 +19,23 @@ public Step(Slice startPosition, Slice delta) {
1919
this.delta = delta;
2020
}
2121

22+
public boolean isValid(Pizza pizza) {
23+
Slice slice = new Slice(new ArrayList<>(startPosition.cells));
24+
slice.cells.addAll(delta.cells);
25+
return slice.isValid(pizza);
26+
}
27+
28+
public int size() {
29+
return startPosition.cells.size() + delta.cells.size();
30+
}
31+
2232
@Override
2333
public String toString() {
24-
return "Step{" +
25-
"startPosition=" + startPosition +
26-
", delta=" + delta +
27-
'}';
34+
return "\nStep{" +
35+
"\nstartPosition=" + startPosition +
36+
"\ndelta=" + delta +
37+
"\n}";
2838
}
2939

30-
public static boolean isValid(Pizza pizza, Slice startSlice, Slice checkedSlice){
31-
if( pizza.containsCells(checkedSlice) &&
32-
startSlice.cells.size() + checkedSlice.cells.size() <= pizza.getSliceInstruction().getMaxNumberOfCellsPerSlice()){
33-
return true;
34-
}
35-
36-
return false;
37-
}
3840

3941
}

src/main/java/com/google/hashcode/service/Slicer.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
package com.google.hashcode.service;
22

3-
import com.google.hashcode.entity.*;
4-
import org.slf4j.Logger;
5-
import org.slf4j.LoggerFactory;
6-
7-
import java.util.Arrays;
8-
import java.util.LinkedList;
9-
import java.util.List;
10-
import java.util.stream.Collectors;
11-
12-
import static java.lang.Integer.valueOf;
13-
143
/**
154
* Slice a pizza according to a slice instructions
165
*

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

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
66

7-
import java.util.ArrayList;
8-
import java.util.HashMap;
9-
import java.util.List;
10-
import java.util.Map;
7+
import java.util.*;
118
import java.util.stream.Collectors;
129

1310
public abstract class DFSMethods {
@@ -19,27 +16,29 @@ private DFSMethods() {
1916
/**
2017
* For each slice find all available steps. We DON'T change the pizza on this stage
2118
*
22-
* @param pizza given pizza
23-
* @param startPos given slices in the pizza
19+
* @param pizza given pizza
20+
* @param startPositions given slices in the pizza
2421
* @return available steps
2522
*/
26-
public static Map<Slice, List<Step>> getAvailableSteps(Pizza pizza, List<Slice> startPos) {
23+
public static Map<Slice, List<Step>> getAvailableSteps(Pizza pizza, List<Slice> startPositions) {
2724
Map<Slice, List<Step>> groupedSteps = new HashMap<>();
28-
for (Slice slice : startPos) {
25+
for (Slice startPosition : startPositions) {
2926
List<Step> steps = new ArrayList<>();
30-
Slice stepLeftDelta = slice.generateStepDeltaLeft();
31-
Slice stepRightDelta = slice.generateStepDeltaRight();
32-
Slice stepAboveDelta = slice.generateStepDeltaAbove();
33-
Slice stepBelowDelta = slice.generateStepDeltaBelow();
34-
if (Step.isValid(pizza, slice, stepLeftDelta)) steps.add(new Step(slice, stepLeftDelta));
35-
if (Step.isValid(pizza, slice, stepRightDelta)) steps.add(new Step(slice, stepRightDelta));
36-
if (Step.isValid(pizza, slice, stepAboveDelta)) steps.add(new Step(slice, stepAboveDelta));
37-
if (Step.isValid(pizza, slice, stepBelowDelta)) steps.add(new Step(slice, stepBelowDelta));
38-
groupedSteps.put(slice, steps);
27+
Step stepLeft = startPosition.generateStepLeft(pizza);
28+
Step stepRight = startPosition.generateStepRight(pizza);
29+
Step stepAbove = startPosition.generateStepAbove(pizza);
30+
Step stepBelow = startPosition.generateStepBelow(pizza);
31+
32+
steps.add(stepRight);
33+
steps.add(stepLeft);
34+
steps.add(stepBelow);
35+
steps.add(stepAbove);
36+
steps = steps.stream().filter(Objects::nonNull).collect(Collectors.toList());
37+
38+
groupedSteps.put(startPosition, steps);
3939
}
4040
LOGGER.info("available steps for" +
4141
"\npizza: " + pizza
42-
+ "\nslices: " + startPos
4342
+ "\nsteps: " + groupedSteps);
4443
return groupedSteps;
4544
}
@@ -54,22 +53,23 @@ public static Map<Slice, List<Step>> getAvailableSteps(Pizza pizza, List<Slice>
5453
*/
5554
public static Slice performStep(Pizza pizza, Step step) {
5655
//1. Pick ups a steps list with minimal total cells number
57-
LOGGER.info("step to perform: " + step);
56+
LOGGER.info("STEP TO PERFORM " + step);
5857
//2. Cut all the step delta cells from pizza
5958
LOGGER.info("pizza before step: " + pizza
6059
+ "\ndelta to remove from the pizza: " + step.delta);
6160
pizza.getCells().removeAll(step.delta.cells);
62-
LOGGER.info("pizza after step:" + pizza);
61+
LOGGER.info("PIZZA AFTER STEP:" + pizza);
6362
//3. Add the step cells to an output slice
6463
Slice slice = new Slice(step.delta.cells);
6564
slice.cells.addAll(step.startPosition.cells);
6665
return slice;
6766
}
6867

6968
public static Step selectStep(Map<Slice, List<Step>> steps) {
70-
return steps.values().stream()
71-
.min((o1, o2) -> new StepsComparator().compare(o1, o2)).get()
72-
.get(0);
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;
7373
}
7474

7575
/**
@@ -86,7 +86,8 @@ public static List<Slice> cutAllStartPositions(Pizza pizza) {
8686
List<Cell> tomatoes = pizza.getCells().stream()
8787
.filter(cell -> cell.ingredient.equals(Ingredient.TOMATO))
8888
.collect(Collectors.toList());
89-
LOGGER.info("cutAllStartPositions for pizza: " + pizza
89+
LOGGER.info("cutAllStartPositions for pizza: "
90+
+ "\n" + pizza
9091
+ "\nmushrooms number: " + mushrooms.size()
9192
+ "\ntomatoes number: " + tomatoes.size());
9293
List<Slice> startPositions = null;
@@ -107,7 +108,8 @@ public static List<Slice> cutAllStartPositions(Pizza pizza) {
107108
.collect(Collectors.toList());
108109
pizza.getCells().removeAll(cellsToRemove);
109110
}
110-
LOGGER.info("pizza without start positions:" + pizza);
111+
LOGGER.info("pizza without start positions:"
112+
+ "\n" + pizza);
111113
return startPositions;
112114
}
113115

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.util.Comparator;
66
import java.util.List;
7+
import java.util.Objects;
78

89
/**
910
* @author Grigoriy Lyashenko (Grog).
@@ -13,11 +14,11 @@ public class StepsComparator implements Comparator<List<Step>> {
1314
@Override
1415
public int compare(List<Step> o1, List<Step> o2) {
1516
long o1CellsCount = o1.stream()
16-
.flatMap(step -> step.delta.cells.stream())
17-
.count();
17+
.mapToLong(step -> step.delta.cells.size())
18+
.sum();
1819
long o2CellsCount = o2.stream()
19-
.flatMap(step -> step.delta.cells.stream())
20-
.count();
20+
.mapToLong(step -> step.delta.cells.size())
21+
.sum();
2122
return Long.compare(o1CellsCount, o2CellsCount);
2223
}
2324
}

0 commit comments

Comments
 (0)