Skip to content

Commit 269544f

Browse files
committed
Merge remote-tracking branch 'remotes/origin/DFSMethod_getAllAvailableSteps' into DFSmethods
2 parents 3de734e + 7275380 commit 269544f

File tree

11 files changed

+247
-71
lines changed

11 files changed

+247
-71
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/Cell.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,8 @@ public int hashCode() {
4141
public int getX() {
4242
return x;
4343
}
44+
45+
public int getY() {
46+
return y;
47+
}
4448
}

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

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

33
import java.io.File;
4+
import java.util.Comparator;
45
import java.util.List;
56
import java.util.Optional;
67

@@ -51,9 +52,41 @@ public SliceInstruction getSliceInstruction() {
5152

5253
@Override
5354
public String toString() {
54-
return input.toString() +
55-
//TODO fix human readable output "\n" + IoUtils.convertToHumanReadableTable(cells) +
56-
"\n" + sliceInstruction.toString();
55+
return input.toString()
56+
+ ("\n" + sliceInstruction.toString()
57+
+ "\n" + outputCellsArray()).trim();
5758
}
5859

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);
68+
}
69+
70+
private String outputCellsArray() {
71+
StringBuilder stringBuilder = new StringBuilder();
72+
int columnsCount = cells.stream().max(Comparator.comparingInt(Cell::getX)).get().getX();
73+
int rowsCount = cells.stream().max(Comparator.comparingInt(Cell::getY)).get().getY();
74+
//output columns coordinates
75+
stringBuilder.append(" ");
76+
for (int column = 0; column < columnsCount + 1; column++) {
77+
stringBuilder.append(" ").append(column);
78+
}
79+
stringBuilder.append("\n");
80+
for (int row = 0; row < rowsCount + 1; row++) {
81+
//output rows coordinates
82+
stringBuilder.append(row).append(" ");
83+
for (int column = 0; column < columnsCount + 1; column++) {
84+
stringBuilder.append(this.getCell(row, column).toString()).append(" ");
85+
}
86+
stringBuilder.append("\n");
87+
}
88+
return stringBuilder.toString();
89+
}
90+
91+
5992
}

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,57 @@ public boolean isValid(Pizza pizza) {
8080
return isPassedSliceInstructions;
8181
}
8282

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);
96+
}
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);
108+
}
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);
120+
}
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);
132+
}
133+
//endregion
134+
83135
}
84136

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,22 @@
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+
}
16+
17+
public Slice startPosition;
18+
19+
public Slice delta;
20+
21+
@Override
22+
public String toString() {
23+
return "Step{" +
24+
"startPosition=" + startPosition +
25+
", delta=" + delta +
26+
'}';
27+
}
1228

13-
public Cell delta;
1429
}

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

Lines changed: 46 additions & 28 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;
@@ -59,38 +54,61 @@ public static Optional<Slice> rightStep(Pizza pizza, Slice slice) {
5954
}
6055

6156
/**
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.
57+
* For each slice find all available steps. We DON'T change the pizza on this stage
6458
*
6559
* @param pizza given pizza
66-
* @param output given slice in the pizza
60+
* @param output given slices in the pizza
6761
* @return available steps
6862
*/
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-
return new ArrayList<>();
63+
public static List<Step> getAvailableSteps(Pizza pizza, List<Slice> output) {
64+
List<Step> steps = new ArrayList<>();
65+
for (Slice slice : output) {
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));
74+
}
75+
LOGGER.info("available steps for" +
76+
"\npizza: " + pizza
77+
+ "\nslices: " + output
78+
+ "\nsteps: " + steps);
79+
return steps;
7280
}
7381

74-
Slice performStep(Pizza pizza, List<Step> steps) {
82+
public static Slice performStep(Pizza pizza, List<Step> steps) {
7583
//TODO pick-ups a step with a minimal steps number, execute it(cut it from the pizza, and a slice)
7684
return null;
7785
}
7886

79-
List<Slice> cutAllStartPositions(Pizza pizza) {
80-
//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
81-
List<Cell> currentPizza = pizza.getCells();
82-
List<Slice> starts = new ArrayList<Slice>();
83-
Iterator<Cell> iter = currentPizza.iterator();
84-
while(iter.hasNext()){
85-
Cell cell = iter.next();
86-
if(cell.ingredient == Ingredient.MUSHROOM){
87-
Slice slice = new Slice();
88-
slice.cells.add(cell);
89-
starts.add(slice);
90-
iter.remove();
91-
}
92-
}
93-
return starts;
87+
/**
88+
* Finds a cells type with minimal cells numbers and generates one cell slices from them
89+
*
90+
* @param pizza given pizza
91+
* @return slices that are start positions for future slicing
92+
*/
93+
public static List<Slice> cutAllStartPositions(Pizza pizza) {
94+
List<Cell> mushrooms = pizza.getCells().stream()
95+
.filter(cell -> cell.ingredient.equals(Ingredient.MUSHROOM))
96+
.collect(Collectors.toList());
97+
List<Cell> tomatoes = pizza.getCells().stream()
98+
.filter(cell -> cell.ingredient.equals(Ingredient.TOMATO))
99+
.collect(Collectors.toList());
100+
LOGGER.info("cutAllStartPositions for pizza: " + pizza
101+
+ "\nmushrooms number: " + mushrooms.size()
102+
+ "\ntomatoes number: " + tomatoes.size());
103+
if (mushrooms.size() > tomatoes.size()) {
104+
return tomatoes.stream()
105+
.map(Slice::new)
106+
.collect(Collectors.toList());
107+
} else {
108+
return mushrooms.stream()
109+
.map(Slice::new)
110+
.collect(Collectors.toList());
111+
}
94112
}
95113

96114
}

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,24 +72,6 @@ public static SliceInstruction parseSliceInstructions(String file) throws IOExce
7272
}
7373
}
7474

75-
/**
76-
* Converts given pizza cells 2d array to human readable string representation
77-
*
78-
* @param ingredients given array
79-
* @return table like String representation
80-
*/
81-
public static String convertToHumanReadableTable(Cell[][] ingredients) {
82-
StringBuilder output = new StringBuilder();
83-
for (Cell[] row : ingredients) {
84-
for (Cell cell : row) {
85-
output.append(cell).append(" ");
86-
}
87-
output.append("\n");
88-
}
89-
return output.toString();
90-
//TODO reimplement for the pizza
91-
}
92-
9375
/**
9476
* Formats data from list of slices to the required output format
9577
*
Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,51 @@
11
package com.google.hashcode.entity;
22

3-
import com.google.hashcode.utils.InputFiles;
43
import com.google.hashcode.utils.IoUtils;
4+
import org.junit.Before;
55
import org.junit.Test;
66

77
import java.io.File;
8+
import java.io.IOException;
89

9-
import static com.google.hashcode.utils.InputFiles.*;
10+
import static com.google.hashcode.utils.InputFiles.EXAMPLE_INPUT_FILE_PATH;
1011
import static org.junit.Assert.assertEquals;
1112

1213
/**
1314
* @author Grigoriy Lyashenko (Grog).
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);
33+
}
34+
35+
@Test
36+
public void testToString() throws IOException {
37+
assertEquals("inputDataSets/example.inSliceInstructions: \n" +
38+
"min 1 ingredient per slice, max 6 cells per slice \n" +
39+
" 0 1 2 3 4\n" +
40+
"0 T T T T T \n" +
41+
"1 T M M M T \n" +
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+
2749
}
2850

2951
}

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
}

0 commit comments

Comments
 (0)