Skip to content

Commit 7275380

Browse files
committed
DFSMethod_getAllAvailableSteps. Partially fix getAllavailableSteps
1 parent fe1f5bf commit 7275380

File tree

6 files changed

+122
-77
lines changed

6 files changed

+122
-77
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,16 @@ public String toString() {
5757
+ "\n" + outputCellsArray()).trim();
5858
}
5959

60-
public boolean cotnainsAllCells(Slice slice){
61-
return slice.cells.stream().allMatch(cell->this.cells.contains(cell));
60+
/**
61+
* Indicates does this pizza contains each slice's cell
62+
*
63+
* @param slice given slice
64+
* @return true if the pizza contains the slice
65+
*/
66+
public boolean containsCells(Slice slice) {
67+
return slice.cells.stream().allMatch(this.cells::contains);
6268
}
6369

64-
6570
private String outputCellsArray() {
6671
StringBuilder stringBuilder = new StringBuilder();
6772
int columnsCount = cells.stream().max(Comparator.comparingInt(Cell::getX)).get().getX();

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

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -79,54 +79,58 @@ public boolean isValid(Pizza pizza) {
7979
"\npassed validation: " + isPassedSliceInstructions);
8080
return isPassedSliceInstructions;
8181
}
82-
83-
public Slice generateAbowe(Slice slice){
84-
List<Cell> cells = new ArrayList<Cell>();
85-
int maxX = slice.maxX();
86-
int minX = slice.minX();
87-
int minY = slice.minY();
88-
for(int i = minX; i <= maxX; i++){
89-
Cell cell = new Cell(minY-1, i, null);
90-
cells.add(cell);
91-
}
92-
return new Slice(cells);
82+
83+
//region generate steps
84+
85+
86+
public Slice generateStepDeltaAbove() {
87+
List<Cell> delta = new ArrayList<>();
88+
for (int x = this.minX(); x <= this.maxX(); x++) {
89+
Cell cell = new Cell(this.minY() - 1, x, Ingredient.TOMATO);
90+
delta.add(cell);
91+
}
92+
LOGGER.info("generateStepDeltaAbove"
93+
+ "\nslice :" + this.toString()
94+
+ "\nstep above delta: " + delta.toString());
95+
return new Slice(delta);
9396
}
94-
95-
public Slice generateBelow(Slice slice){
96-
List<Cell> cells = new ArrayList<Cell>();
97-
int maxX = slice.maxX();
98-
int minX = slice.minX();
99-
int maxY = slice.maxY();
100-
for(int i = minX; i <= maxX; i++){
101-
Cell cell = new Cell(maxY+1, i, null);
102-
cells.add(cell);
103-
}
104-
return new Slice(cells);
97+
98+
public Slice generateStepDeltaBelow() {
99+
List<Cell> delta = new ArrayList<>();
100+
for (int x = this.minX(); x <= this.maxX(); x++) {
101+
Cell cell = new Cell(this.maxY() + 1, x, Ingredient.TOMATO);
102+
delta.add(cell);
103+
}
104+
LOGGER.info("generateStepDeltaBelow"
105+
+ "\nslice :" + this.toString()
106+
+ "\nstep below delta: " + delta.toString());
107+
return new Slice(delta);
105108
}
106-
107-
public Slice generateLeft(Slice slice){
108-
List<Cell> cells = new ArrayList<Cell>();
109-
int minX = slice.minX();
110-
int minY = slice.minY();
111-
int maxY = slice.maxY();
112-
for(int i = minY; i <= maxY; i++){
113-
Cell cell = new Cell(i, minX-1, null);
114-
cells.add(cell);
115-
}
116-
return new Slice(cells);
109+
110+
public Slice generateStepDeltaLeft() {
111+
List<Cell> delta = new ArrayList<>();
112+
for (int y = this.minY(); y <= this.maxY(); y++) {
113+
Cell cell = new Cell(y, minX(), Ingredient.TOMATO);
114+
delta.add(cell);
115+
}
116+
LOGGER.info("generateStepDeltaLeft"
117+
+ "\nslice :" + this.toString()
118+
+ "\nstep left delta: " + delta.toString());
119+
return new Slice(delta);
117120
}
118-
119-
public Slice generateRight(Slice slice){
120-
List<Cell> cells = new ArrayList<Cell>();
121-
int maxX = slice.maxX();
122-
int minY = slice.minY();
123-
int maxY = slice.maxY();
124-
for(int i = minY; i <= maxY; i++){
125-
Cell cell = new Cell(i, maxX+1, null);
126-
cells.add(cell);
127-
}
128-
return new Slice(cells);
121+
122+
public Slice generateStepRight() {
123+
List<Cell> delta = new ArrayList<>();
124+
for (int y = this.minY(); y <= this.maxY(); y++) {
125+
Cell cell = new Cell(y, minX(), Ingredient.TOMATO);
126+
delta.add(cell);
127+
}
128+
LOGGER.info("generateStepDeltaRight"
129+
+ "\nslice :" + this.toString()
130+
+ "\nstep right delta: " + delta.toString());
131+
return new Slice(delta);
129132
}
133+
//endregion
130134

131135
}
132136

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

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
package com.google.hashcode.utils;
22

3-
import com.google.hashcode.entity.Cell;
4-
import com.google.hashcode.entity.Ingredient;
5-
import com.google.hashcode.entity.Pizza;
6-
import com.google.hashcode.entity.Slice;
7-
import com.google.hashcode.entity.Step;
3+
import com.google.hashcode.entity.*;
84
import org.slf4j.Logger;
95
import org.slf4j.LoggerFactory;
106

117
import java.util.ArrayList;
12-
import java.util.Iterator;
138
import java.util.List;
149
import java.util.Optional;
1510
import java.util.stream.Collectors;
@@ -68,14 +63,14 @@ public static Optional<Slice> rightStep(Pizza pizza, Slice slice) {
6863
public static List<Step> getAvailableSteps(Pizza pizza, List<Slice> output) {
6964
List<Step> steps = new ArrayList<>();
7065
for (Slice slice : output) {
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));
66+
Slice stepLeftDelta = slice.generateStepDeltaLeft();
67+
Slice stepRightDelta = slice.generateStepRight();
68+
Slice stepAboveDelta = slice.generateStepDeltaAbove();
69+
Slice stepBelowDelta = slice.generateStepDeltaBelow();
70+
if (pizza.containsCells(stepLeftDelta)) steps.add(new Step(slice, stepLeftDelta));
71+
if (pizza.containsCells(stepRightDelta)) steps.add(new Step(slice, stepRightDelta));
72+
if (pizza.containsCells(stepAboveDelta)) steps.add(new Step(slice, stepAboveDelta));
73+
if (pizza.containsCells(stepBelowDelta)) steps.add(new Step(slice, stepBelowDelta));
7974
}
8075
LOGGER.info("available steps for" +
8176
"\npizza: " + pizza
Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.google.hashcode.entity;
22

33
import com.google.hashcode.utils.IoUtils;
4+
import org.junit.Before;
45
import org.junit.Test;
56

67
import java.io.File;
@@ -14,28 +15,37 @@
1415
*/
1516
public class PizzaTest {
1617

18+
private Pizza examplePizza;
19+
20+
@Before
21+
public void setup() throws IOException {
22+
examplePizza = new Pizza(new File(EXAMPLE_INPUT_FILE_PATH), IoUtils.parsePizza(EXAMPLE_INPUT_FILE_PATH), IoUtils.parseSliceInstructions(EXAMPLE_INPUT_FILE_PATH));
23+
}
24+
1725
@Test
1826
public void getCell() throws Exception {
19-
Pizza pizza = new Pizza(new File(EXAMPLE_INPUT_FILE_PATH), IoUtils.parsePizza(EXAMPLE_INPUT_FILE_PATH), IoUtils.parseSliceInstructions(EXAMPLE_INPUT_FILE_PATH));
20-
assertEquals(pizza.getCell(0, 0), new Cell(0, 0, Ingredient.TOMATO));
27+
assertEquals(examplePizza.getCell(0, 0), new Cell(0, 0, Ingredient.TOMATO));
2128
}
2229

2330
@Test(expected = IllegalArgumentException.class)
2431
public void getCellException() throws Exception {
25-
Pizza pizza = new Pizza(new File(EXAMPLE_INPUT_FILE_PATH), IoUtils.parsePizza(EXAMPLE_INPUT_FILE_PATH), IoUtils.parseSliceInstructions(EXAMPLE_INPUT_FILE_PATH));
26-
pizza.getCell(100500, 0);
32+
examplePizza.getCell(100500, 0);
2733
}
2834

2935
@Test
3036
public void testToString() throws IOException {
31-
Pizza pizza = new Pizza(new File(EXAMPLE_INPUT_FILE_PATH), IoUtils.parsePizza(EXAMPLE_INPUT_FILE_PATH), IoUtils.parseSliceInstructions(EXAMPLE_INPUT_FILE_PATH));
32-
System.out.println(pizza);
3337
assertEquals("inputDataSets/example.inSliceInstructions: \n" +
3438
"min 1 ingredient per slice, max 6 cells per slice \n" +
3539
" 0 1 2 3 4\n" +
3640
"0 T T T T T \n" +
3741
"1 T M M M T \n" +
38-
"2 T T T T T", pizza.toString());
42+
"2 T T T T T", examplePizza.toString());
43+
}
44+
45+
@Test
46+
public void containsCells() {
47+
//given a slice based on the pizza cells
48+
3949
}
4050

4151
}

src/test/java/com/google/hashcode/entity/SliceTest.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
import java.util.Arrays;
88

99
import static com.google.hashcode.utils.InputFiles.EXAMPLE_INPUT_FILE_PATH;
10-
import static org.junit.Assert.assertFalse;
11-
import static org.junit.Assert.assertTrue;
10+
import static org.junit.Assert.*;
1211

1312
/**
1413
* @author Grigoriy Lyashenko (Grog).
@@ -21,8 +20,41 @@ public void isValid() throws Exception {
2120
Slice invalidSlice = new Slice(pizza.getCells());
2221
assertFalse(invalidSlice.isValid(pizza));
2322
//create a valid slice
24-
Slice validSlice = new Slice(Arrays.asList(new Cell(0, 0, Ingredient.TOMATO), new Cell(0, 1, Ingredient.MUSHROOM)));
23+
Slice validSlice = new Slice(Arrays.asList(
24+
new Cell(0, 0, Ingredient.TOMATO),
25+
new Cell(0, 1, Ingredient.MUSHROOM)));
2526
assertTrue(validSlice.isValid(pizza));
2627
}
2728

29+
@Test
30+
public void generateStepDeltaBelow() {
31+
Slice slice = new Slice(Arrays.asList(
32+
new Cell(0, 0, Ingredient.MUSHROOM),
33+
new Cell(0, 1, Ingredient.TOMATO)));
34+
assertEquals(2, slice.generateStepDeltaBelow().cells.size());
35+
}
36+
37+
@Test
38+
public void generateStepDeltaAbove() {
39+
Slice slice = new Slice(Arrays.asList(
40+
new Cell(0, 0, Ingredient.MUSHROOM),
41+
new Cell(0, 1, Ingredient.TOMATO)));
42+
assertEquals(2, slice.generateStepDeltaAbove().cells.size());
43+
}
44+
45+
@Test
46+
public void generateStepDeltaLeft() {
47+
Slice slice = new Slice(Arrays.asList(
48+
new Cell(0, 0, Ingredient.MUSHROOM),
49+
new Cell(0, 1, Ingredient.TOMATO)));
50+
assertEquals(1, slice.generateStepDeltaLeft().cells.size());
51+
}
52+
53+
@Test
54+
public void generateStepRight() {
55+
Slice slice = new Slice(Arrays.asList(
56+
new Cell(0, 0, Ingredient.MUSHROOM),
57+
new Cell(0, 1, Ingredient.TOMATO)));
58+
assertEquals(1, slice.generateStepRight().cells.size());
59+
}
2860
}

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.google.hashcode.entity.Ingredient;
55
import com.google.hashcode.entity.Pizza;
66
import com.google.hashcode.entity.Slice;
7-
import org.junit.Assert;
87
import org.junit.Test;
98

109
import java.io.File;
@@ -14,7 +13,7 @@
1413
import java.util.List;
1514

1615
import static com.google.hashcode.utils.InputFiles.EXAMPLE_INPUT_FILE_PATH;
17-
import static org.junit.Assert.*;
16+
import static org.junit.Assert.assertEquals;
1817

1918
/**
2019
* @author Grigoriy Lyashenko (Grog).
@@ -32,15 +31,15 @@ public void rightStep() throws Exception {
3231
//and slice has been removed from the pizza
3332
ArrayList<Cell> expectedPizzaCells = new ArrayList<>(pizza.getCells());
3433
expectedPizzaCells.removeAll(expectedSlice.cells);
35-
assertEquals(expectedPizzaCells,pizza.getCells());
34+
assertEquals(expectedPizzaCells, pizza.getCells());
3635
}
3736

3837
@Test
3938
public void getAvailableSteps() throws IOException {
4039
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-
}
40+
/* TODO find a bug and fix assertEquals(8, DFSMethods.
41+
getAvailableSteps(pizza, DFSMethods.cutAllStartPositions(pizza)).size());
42+
*/}
4443

4544
@Test
4645
public void cutAllStartPositions() throws IOException {

0 commit comments

Comments
 (0)