Skip to content

Commit e5b0598

Browse files
authored
Merge pull request #14 from LyashenkoGS/returnUnvalidSlice
returnUnvalidSlices
2 parents 996dca3 + 0e02504 commit e5b0598

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@ private DFSMethods() {
1414
}
1515

1616
/**
17-
* For each slice find all available steps. We DON'T change the pizza on this stage
18-
*
17+
* For each slice find all available steps.<br>
18+
* If founded start position that haven't any steps and it is unvalid ->
19+
* remove this slice from startPositions and add all it's cells to pizza.
1920
* @param pizza given pizza
2021
* @param startPositions given slices in the pizza
2122
* @return available steps
2223
*/
2324
public static Map<Slice, List<Step>> getAvailableSteps(Pizza pizza, List<Slice> startPositions) {
2425
Map<Slice, List<Step>> groupedSteps = new HashMap<>();
25-
for (Slice startPosition : startPositions) {
26+
Iterator iter = startPositions.iterator();
27+
while (iter.hasNext()) {
28+
Slice startPosition = (Slice) iter.next();
29+
2630
List<Step> steps = new ArrayList<>();
2731
Step stepLeft = startPosition.generateStepLeft(pizza);
2832
Step stepRight = startPosition.generateStepRight(pizza);
@@ -35,7 +39,20 @@ public static Map<Slice, List<Step>> getAvailableSteps(Pizza pizza, List<Slice>
3539
steps.add(stepAbove);
3640
steps = steps.stream().filter(Objects::nonNull).collect(Collectors.toList());
3741

38-
groupedSteps.put(startPosition, steps);
42+
if (steps.size() == 0) {
43+
if (startPosition.isValid(pizza)) {
44+
// if slice is valid and have'nt any steps -> cut it from
45+
// startPositions
46+
groupedSteps.put(startPosition, steps);
47+
} else {
48+
// if slice isn't valid and have'nt any steps -> return all
49+
// it cells to pizza
50+
pizza.getCells().addAll(startPosition.cells);
51+
iter.remove();
52+
}
53+
} else {
54+
groupedSteps.put(startPosition, steps);
55+
}
3956
}
4057
LOGGER.info("available steps for" +
4158
"\npizza: " + pizza

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ public static List<Cell> parsePizza(String file) throws IOException {
4040
List<Cell> cells = new ArrayList<>();
4141
int row = 0;
4242
String fileLine;
43+
int counter = 0;
4344
while ((fileLine = br.readLine()) != null) {
4445
for (int column = 0; column < fileLine.length(); column++) {
4546
Character literal = fileLine.charAt(column);
47+
counter ++;
48+
System.out.println("letter " + literal + " counter = " + counter);
4649
if (literal.toString().equals(Ingredient.TOMATO.toString())) {
4750
cells.add(new Cell(row, column, Ingredient.TOMATO));
4851
} else if (literal.toString().equals(Ingredient.MUSHROOM.toString())) {

0 commit comments

Comments
 (0)