Skip to content

Commit 4de30d3

Browse files
committed
DFsmethods. Fix the stepRight feature
1 parent 605bcb3 commit 4de30d3

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,19 @@ public abstract class DFSMethods {
1818
private DFSMethods() {
1919
}
2020

21+
2122
/**
22-
* Calculates a number of cells around the given slice. Available cells is cells that can be merged
23-
* into the slice, so it will remain a rectangle, so only up,down,left,right around an each slice outside cell.
24-
* No on the diagonal !
23+
* Step as an entity is an amount of a pizza cells, that can be added to a slice of a pizza or a pizza cell.<br>
24+
* Step as an action is a process of:<br>
25+
* <p>
26+
* * adding cells to a start cell or a start slice<br>
27+
* * validate generated slice according to the pizza slicing instructions<br>
28+
* * if validation passed ->cutting the start cell with added cells from the pizza
2529
*
26-
* @param slice given pizza
27-
* @param pizza given slice
28-
* @return number of cells available to merge into the slice
30+
* @param pizza (mutable) a pizza to perform step on
31+
* @param slice start position for a step
32+
* @return cutted slice from the pizza
2933
*/
30-
public static int calculateNumberOfFreeCellsAroundSlice(Slice slice, Pizza pizza) {
31-
32-
return 0;
33-
}
34-
3534
public static Optional<Slice> rightStep(Pizza pizza, Slice slice) {
3635
List<Cell> slicesRightBorder = slice.cells.stream()
3736
.filter(cell -> (cell.x == slice.maxX()) && (cell.y >= slice.minY()) && (cell.y <= slice.maxY()))
@@ -45,6 +44,8 @@ public static Optional<Slice> rightStep(Pizza pizza, Slice slice) {
4544
Slice sliceAndStep = new Slice(new ArrayList<>(slice.cells));
4645
sliceAndStep.cells.addAll(step.cells);
4746
if (!slice.cells.isEmpty() && sliceAndStep.isValid(pizza)) {
47+
//remove the slice and step from the pizza
48+
pizza.getCells().removeAll(sliceAndStep.cells);
4849
return Optional.of(sliceAndStep);
4950
} else {
5051
return Optional.empty();

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
import org.junit.Test;
99

1010
import java.io.File;
11+
import java.util.ArrayList;
1112
import java.util.Arrays;
1213

1314
import static com.google.hashcode.utils.InputFiles.EXAMPLE_INPUT_FILE_PATH;
15+
import static org.junit.Assert.*;
1416

1517
/**
1618
* @author Grigoriy Lyashenko (Grog).
@@ -22,9 +24,14 @@ public void rightStep() throws Exception {
2224
//Given a pizza
2325
Pizza pizza = new Pizza(new File(EXAMPLE_INPUT_FILE_PATH), IoUtils.parsePizza(EXAMPLE_INPUT_FILE_PATH), IoUtils.parseSliceInstructions(EXAMPLE_INPUT_FILE_PATH));
2426
//then perform a step right from a particular cell
25-
Slice actual = DFSMethods.rightStep(pizza, new Slice(pizza.getCell(1, 3))).get();
26-
Slice expected = new Slice(Arrays.asList(new Cell(1, 3, Ingredient.MUSHROOM), new Cell(1, 4, Ingredient.TOMATO)));
27-
Assert.assertEquals(expected, actual);
27+
Slice actualSlice = DFSMethods.rightStep(pizza, new Slice(pizza.getCell(1, 3))).get();
28+
Slice expectedSlice = new Slice(Arrays.asList(new Cell(1, 3, Ingredient.MUSHROOM), new Cell(1, 4, Ingredient.TOMATO)));
29+
assertEquals(expectedSlice, actualSlice);
30+
//and slice has been removed from the pizza
31+
ArrayList<Cell> expectedPizzaCells = new ArrayList<>(pizza.getCells());
32+
expectedPizzaCells.removeAll(expectedSlice.cells);
33+
assertEquals(expectedPizzaCells,pizza.getCells());
34+
2835
}
2936

3037
}

0 commit comments

Comments
 (0)