Skip to content

Commit 595dd8f

Browse files
committed
DFSMethod_getAllAvailableSteps. Implement the getAvailableSteps method scratch.
1 parent d71f458 commit 595dd8f

File tree

4 files changed

+74
-42
lines changed

4 files changed

+74
-42
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
.idea
33
*.iml
44
.project
5+
.classpath
6+
.project
57
target
68
#ignore the archived source code
7-
*.zip
9+
*.zip
10+
/.classpath
11+
.settings/

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,13 @@ public Step(Slice startPosition, Slice delta) {
1717
public Slice startPosition;
1818

1919
public Slice delta;
20+
21+
@Override
22+
public String toString() {
23+
return "Step{" +
24+
"startPosition=" + startPosition +
25+
", delta=" + delta +
26+
'}';
27+
}
28+
2029
}

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

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -59,49 +59,61 @@ public static Optional<Slice> rightStep(Pizza pizza, Slice slice) {
5959
}
6060

6161
/**
62-
* Step as an action it's a process, when a slice adding to itself a subset of a pizza cells and remains rectangular
63-
* and valid.
62+
* For each slice find all available steps. We DON'T change the pizza on this stage
6463
*
6564
* @param pizza given pizza
66-
* @param output given slice in the pizza
65+
* @param output given slices in the pizza
6766
* @return available steps
6867
*/
69-
List<Step> getAvailableSteps(Pizza pizza, List<Slice> output) {
70-
//TODO implement.For each slice find all available steps. We DON'T change the pizza on this stage
71-
List<Step> allSteps = new ArrayList<Step>();
68+
public static List<Step> getAvailableSteps(Pizza pizza, List<Slice> output) {
69+
List<Step> steps = new ArrayList<>();
7270
for (Slice slice : output) {
73-
Slice left = slice.generateLeft(slice);
74-
Slice right = slice.generateRight(slice);
75-
Slice abowe = slice.generateAbowe(slice);
76-
Slice belowe = slice.generateBelow(slice);
77-
if(pizza.cotnainsAllCells(left))allSteps.add(new Step(slice, left));
78-
if(pizza.cotnainsAllCells(right))allSteps.add(new Step(slice, right));
79-
if(pizza.cotnainsAllCells(abowe))allSteps.add(new Step(slice, abowe));
80-
if(pizza.cotnainsAllCells(belowe))allSteps.add(new Step(slice, belowe));
81-
}
82-
return allSteps;
71+
Slice left = slice.generateLeft(slice);
72+
Slice right = slice.generateRight(slice);
73+
Slice above = slice.generateAbowe(slice);
74+
Slice below = slice.generateBelow(slice);
75+
if (pizza.cotnainsAllCells(left)) steps.add(new Step(slice, left));
76+
if (pizza.cotnainsAllCells(right)) steps.add(new Step(slice, right));
77+
if (pizza.cotnainsAllCells(above)) steps.add(new Step(slice, above));
78+
if (pizza.cotnainsAllCells(below)) steps.add(new Step(slice, below));
79+
}
80+
LOGGER.info("available steps for" +
81+
"\npizza: " + pizza
82+
+ "\nslices: " + output
83+
+ "\nsteps: " + steps);
84+
return steps;
8385
}
8486

85-
Slice performStep(Pizza pizza, List<Step> steps) {
87+
public static Slice performStep(Pizza pizza, List<Step> steps) {
8688
//TODO pick-ups a step with a minimal steps number, execute it(cut it from the pizza, and a slice)
8789
return null;
8890
}
8991

90-
List<Slice> cutAllStartPositions(Pizza pizza) {
91-
//TODO pick-ups a step with a minimal steps number, execute it(cut it from the pizza, and a slice). The pizza is a mutable object
92-
List<Cell> currentPizza = pizza.getCells();
93-
List<Slice> starts = new ArrayList<Slice>();
94-
Iterator<Cell> iter = currentPizza.iterator();
95-
while(iter.hasNext()){
96-
Cell cell = iter.next();
97-
if(cell.ingredient == Ingredient.MUSHROOM){
98-
Slice slice = new Slice();
99-
slice.cells.add(cell);
100-
starts.add(slice);
101-
iter.remove();
102-
}
103-
}
104-
return starts;
92+
/**
93+
* Finds a cells type with minimal cells numbers and generates one cell slices from them
94+
*
95+
* @param pizza given pizza
96+
* @return slices that are start positions for future slicing
97+
*/
98+
public static List<Slice> cutAllStartPositions(Pizza pizza) {
99+
List<Cell> mushrooms = pizza.getCells().stream()
100+
.filter(cell -> cell.ingredient.equals(Ingredient.MUSHROOM))
101+
.collect(Collectors.toList());
102+
List<Cell> tomatoes = pizza.getCells().stream()
103+
.filter(cell -> cell.ingredient.equals(Ingredient.TOMATO))
104+
.collect(Collectors.toList());
105+
LOGGER.info("cutAllStartPositions for pizza: " + pizza
106+
+ "\nmushrooms number: " + mushrooms.size()
107+
+ "\ntomatoes number: " + tomatoes.size());
108+
if (mushrooms.size() > tomatoes.size()) {
109+
return tomatoes.stream()
110+
.map(Slice::new)
111+
.collect(Collectors.toList());
112+
} else {
113+
return mushrooms.stream()
114+
.map(Slice::new)
115+
.collect(Collectors.toList());
116+
}
105117
}
106118

107119
}

src/test/java/com/google/hashcode/utils/DFSMethodsTest.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,23 @@ public void rightStep() throws Exception {
3434
expectedPizzaCells.removeAll(expectedSlice.cells);
3535
assertEquals(expectedPizzaCells,pizza.getCells());
3636
}
37-
37+
38+
@Test
39+
public void getAvailableSteps() throws IOException {
40+
Pizza pizza = new Pizza(new File(EXAMPLE_INPUT_FILE_PATH), IoUtils.parsePizza(EXAMPLE_INPUT_FILE_PATH), IoUtils.parseSliceInstructions(EXAMPLE_INPUT_FILE_PATH));
41+
//TODO implement the method properly ! assertEquals(8,DFSMethods.
42+
// getAvailableSteps(pizza,DFSMethods.cutAllStartPositions(pizza)).size());
43+
}
44+
3845
@Test
39-
public void getAvailableSteps() throws IOException{
40-
Pizza pizza = new Pizza(new File(EXAMPLE_INPUT_FILE_PATH), IoUtils.parsePizza(EXAMPLE_INPUT_FILE_PATH), IoUtils.parseSliceInstructions(EXAMPLE_INPUT_FILE_PATH));
41-
Slice a = new Slice(new Cell(1, 1, Ingredient.MUSHROOM));
42-
Slice b = new Slice(new Cell(0, 0, Ingredient.MUSHROOM));
43-
List<Slice> slices = new ArrayList<Slice>();
44-
slices.add(a);
45-
slices.add(b);
46-
// generate expected slices
46+
public void cutAllStartPositions() throws IOException {
47+
Pizza pizza = new Pizza(new File(EXAMPLE_INPUT_FILE_PATH), IoUtils.parsePizza(EXAMPLE_INPUT_FILE_PATH), IoUtils.parseSliceInstructions(EXAMPLE_INPUT_FILE_PATH));
48+
List<Slice> expected = Arrays.asList(
49+
new Slice(new Cell(1, 1, Ingredient.MUSHROOM)),
50+
new Slice(new Cell(1, 2, Ingredient.MUSHROOM)),
51+
new Slice(new Cell(1, 3, Ingredient.MUSHROOM))
52+
);
53+
assertEquals(expected, DFSMethods.cutAllStartPositions(pizza));
4754
}
4855

4956
}

0 commit comments

Comments
 (0)