Skip to content

Commit d71f458

Browse files
committed
Generate all available steps
1 parent 3de734e commit d71f458

File tree

5 files changed

+86
-5
lines changed

5 files changed

+86
-5
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,9 @@ public String toString() {
5555
//TODO fix human readable output "\n" + IoUtils.convertToHumanReadableTable(cells) +
5656
"\n" + sliceInstruction.toString();
5757
}
58-
58+
59+
public boolean cotnainsAllCells(Slice slice){
60+
return slice.cells.stream().allMatch(cell->this.cells.contains(cell));
61+
}
62+
5963
}

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,54 @@ 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);
93+
}
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);
105+
}
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);
117+
}
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);
129+
}
82130

83131
}
84132

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88
*/
99
public class Step {
1010

11-
public Slice startPosition;
11+
public Step(Slice startPosition, Slice delta) {
12+
super();
13+
this.startPosition = startPosition;
14+
this.delta = delta;
15+
}
1216

13-
public Cell delta;
17+
public Slice startPosition;
18+
19+
public Slice delta;
1420
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,18 @@ public static Optional<Slice> rightStep(Pizza pizza, Slice slice) {
6868
*/
6969
List<Step> getAvailableSteps(Pizza pizza, List<Slice> output) {
7070
//TODO implement.For each slice find all available steps. We DON'T change the pizza on this stage
71-
return new ArrayList<>();
71+
List<Step> allSteps = new ArrayList<Step>();
72+
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;
7283
}
7384

7485
Slice performStep(Pizza pizza, List<Step> steps) {

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
import org.junit.Test;
99

1010
import java.io.File;
11+
import java.io.IOException;
1112
import java.util.ArrayList;
1213
import java.util.Arrays;
14+
import java.util.List;
1315

1416
import static com.google.hashcode.utils.InputFiles.EXAMPLE_INPUT_FILE_PATH;
1517
import static org.junit.Assert.*;
@@ -31,7 +33,17 @@ public void rightStep() throws Exception {
3133
ArrayList<Cell> expectedPizzaCells = new ArrayList<>(pizza.getCells());
3234
expectedPizzaCells.removeAll(expectedSlice.cells);
3335
assertEquals(expectedPizzaCells,pizza.getCells());
34-
36+
}
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+
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
3547
}
3648

3749
}

0 commit comments

Comments
 (0)