Skip to content

Commit 605bcb3

Browse files
committed
DFSmethod. Add basic step implementation
1 parent f372081 commit 605bcb3

File tree

7 files changed

+125
-55
lines changed

7 files changed

+125
-55
lines changed

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

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
package com.google.hashcode.entity;
22

3+
import java.util.Objects;
4+
35
/**
46
* Represents a pizza cell with it coordinates. There is no getters/setters for simplicity
57
*
68
* @author Grigoriy Lyashenko (Grog).
79
*/
810
public class Cell {
9-
public int x;
1011
public int y;
12+
public int x;
1113
public Ingredient ingredient;
12-
/**
13-
* indicates if given cell has been sliced
14-
*/
15-
public boolean sliced = false;
1614

17-
public Cell(int x, int y, Ingredient ingredient) {
18-
this.x = x;
15+
public Cell(int y, int x, Ingredient ingredient) {
1916
this.y = y;
17+
this.x = x;
2018
this.ingredient = ingredient;
2119
}
2220

@@ -25,27 +23,19 @@ public String toString() {
2523
return ingredient.toString();
2624
}
2725

28-
public static Cell prototype(int x, int y) {
29-
return new Cell(x, y, null);
30-
}
31-
3226
@Override
3327
public boolean equals(Object o) {
3428
if (this == o) return true;
3529
if (!(o instanceof Cell)) return false;
36-
3730
Cell cell = (Cell) o;
38-
39-
if (x != cell.x) return false;
40-
if (y != cell.y) return false;
41-
return true;
31+
return x == cell.x &&
32+
y == cell.y &&
33+
ingredient == cell.ingredient;
4234
}
4335

4436
@Override
4537
public int hashCode() {
46-
int result = x;
47-
result = 31 * result + y;
48-
return result;
38+
return Objects.hash(x, y, ingredient);
4939
}
5040

5141
public int getX() {
Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package com.google.hashcode.entity;
22

3-
import java.util.ArrayList;
4-
import java.util.Collections;
5-
import java.util.Comparator;
6-
import java.util.List;
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import java.util.*;
7+
import java.util.stream.Collectors;
78

89
/**
910
* A rectangle piece of a pizza
1011
*
1112
* @author Grigoriy Lyashenko (Grog).
1213
*/
1314
public class Slice {
15+
private static final Logger LOGGER = LoggerFactory.getLogger(Slice.class);
1416

1517
public List<Cell> cells = new ArrayList<>();
1618

@@ -21,6 +23,23 @@ public Slice(Cell cell) {
2123
this.cells = Collections.singletonList(cell);
2224
}
2325

26+
public Slice(List<Cell> cells) {
27+
this.cells = cells;
28+
}
29+
30+
@Override
31+
public boolean equals(Object o) {
32+
if (this == o) return true;
33+
if (!(o instanceof Slice)) return false;
34+
Slice slice = (Slice) o;
35+
return Objects.equals(cells, slice.cells);
36+
}
37+
38+
@Override
39+
public int hashCode() {
40+
return Objects.hash(cells);
41+
}
42+
2443
public int minX() {
2544
return Collections.min(cells, Comparator.comparingInt(Cell::getX)).x;
2645
}
@@ -33,11 +52,6 @@ public int maxX() {
3352
return Collections.max(cells, Comparator.comparingInt(Cell::getX)).x;
3453
}
3554

36-
37-
/* public Slice(Cell cell) {
38-
this.cells = Collections.singletonList(cell);
39-
}*/
40-
4155
public int maxY() {
4256
return Collections.max(cells, Comparator.comparingInt(Cell::getX)).y;
4357
}
@@ -46,5 +60,25 @@ public int maxY() {
4660
public String toString() {
4761
return cells.toString();
4862
}
63+
64+
public boolean isValid(Pizza pizza) {
65+
//TODO check rectangularity
66+
int mushroomsNumber = this.cells.stream()
67+
.filter(cell -> cell.ingredient.equals(Ingredient.MUSHROOM))
68+
.collect(Collectors.toList())
69+
.size();
70+
int tomatoesNumber = this.cells.stream()
71+
.filter(cell -> cell.ingredient.equals(Ingredient.TOMATO))
72+
.collect(Collectors.toList())
73+
.size();
74+
boolean isPassedSliceInstructions = this.cells.size() <= pizza.getSliceInstruction().getMaxNumberOfCellsPerSlice()
75+
&& tomatoesNumber >= pizza.getSliceInstruction().getMinNumberOfIngredientPerSlice()
76+
&& mushroomsNumber >= pizza.getSliceInstruction().getMinNumberOfIngredientPerSlice();
77+
LOGGER.info("\n" + pizza.getSliceInstruction() +
78+
"\nSlice :" + this +
79+
"\npassed validation: " + isPassedSliceInstructions);
80+
return isPassedSliceInstructions;
81+
}
82+
4983
}
5084

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
import com.google.hashcode.entity.Pizza;
55
import com.google.hashcode.entity.Slice;
66
import com.google.hashcode.entity.SliceInstruction;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
79

810
import java.util.ArrayList;
911
import java.util.List;
1012
import java.util.Optional;
1113
import java.util.stream.Collectors;
1214

1315
public abstract class DFSMethods {
16+
private static final Logger LOGGER = LoggerFactory.getLogger(DFSMethods.class);
17+
1418
private DFSMethods() {
1519
}
1620

@@ -29,17 +33,27 @@ public static int calculateNumberOfFreeCellsAroundSlice(Slice slice, Pizza pizza
2933
}
3034

3135
public static Optional<Slice> rightStep(Pizza pizza, Slice slice) {
32-
List<Cell> rightSlideBorder = slice.cells.stream()
36+
List<Cell> slicesRightBorder = slice.cells.stream()
3337
.filter(cell -> (cell.x == slice.maxX()) && (cell.y >= slice.minY()) && (cell.y <= slice.maxY()))
3438
.collect(Collectors.toList());
3539
//each cell should have a cell right side of it in the pizza
36-
boolean presentsAvailableCellsInPizza = rightSlideBorder.stream()
37-
.allMatch(cell -> pizza.getCells().contains(Cell.prototype(cell.x, cell.y)));
38-
39-
//check is step is valid
40-
SliceInstruction sliceInstruction = pizza.getSliceInstruction();
41-
42-
return Optional.empty();
40+
try {
41+
Slice step = new Slice(slicesRightBorder.stream()
42+
.map(slicesRightBorderCell -> pizza.getCell(slicesRightBorderCell.y, slicesRightBorderCell.x + 1))
43+
.collect(Collectors.toList()));
44+
//check is step is valid
45+
Slice sliceAndStep = new Slice(new ArrayList<>(slice.cells));
46+
sliceAndStep.cells.addAll(step.cells);
47+
if (!slice.cells.isEmpty() && sliceAndStep.isValid(pizza)) {
48+
return Optional.of(sliceAndStep);
49+
} else {
50+
return Optional.empty();
51+
}
52+
} catch (IllegalArgumentException e) {
53+
//if can't add at least one neccessary cell - > return an empty step
54+
LOGGER.info("Can't perform a step right !");
55+
return Optional.empty();
56+
}
4357
}
4458

4559
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public static List<Cell> parsePizza(String file) throws IOException {
4444
for (int column = 0; column < fileLine.length(); column++) {
4545
Character literal = fileLine.charAt(column);
4646
if (literal.toString().equals(Ingredient.TOMATO.toString())) {
47-
cells.add(new Cell(column, row, Ingredient.TOMATO));
47+
cells.add(new Cell(row, column, Ingredient.TOMATO));
4848
} else if (literal.toString().equals(Ingredient.MUSHROOM.toString())) {
49-
cells.add(new Cell(column, row, Ingredient.MUSHROOM));
49+
cells.add(new Cell(row, column, Ingredient.MUSHROOM));
5050
}
5151
}
5252
row++;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.google.hashcode.entity;
2+
3+
import com.google.hashcode.utils.IoUtils;
4+
import org.junit.Test;
5+
6+
import java.io.File;
7+
import java.util.Arrays;
8+
9+
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;
12+
13+
/**
14+
* @author Grigoriy Lyashenko (Grog).
15+
*/
16+
public class SliceTest {
17+
18+
@Test
19+
public void isValid() throws Exception {
20+
Pizza pizza = new Pizza(new File(EXAMPLE_INPUT_FILE_PATH), IoUtils.parsePizza(EXAMPLE_INPUT_FILE_PATH), IoUtils.parseSliceInstructions(EXAMPLE_INPUT_FILE_PATH));
21+
Slice invalidSlice = new Slice(pizza.getCells());
22+
assertFalse(invalidSlice.isValid(pizza));
23+
//create a valid slice
24+
Slice validSlice = new Slice(Arrays.asList(new Cell(0, 0, Ingredient.TOMATO), new Cell(0, 1, Ingredient.MUSHROOM)));
25+
assertTrue(validSlice.isValid(pizza));
26+
}
27+
28+
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
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;
78
import org.junit.Test;
89

910
import java.io.File;
11+
import java.util.Arrays;
12+
13+
import static com.google.hashcode.utils.InputFiles.EXAMPLE_INPUT_FILE_PATH;
1014

1115
/**
1216
* @author Grigoriy Lyashenko (Grog).
@@ -16,11 +20,11 @@ public class DFSMethodsTest {
1620
@Test
1721
public void rightStep() throws Exception {
1822
//Given a pizza
19-
String exampleInputFile = "inputDataSets/example.in";
20-
Pizza pizza = new Pizza(new File(exampleInputFile), IoUtils.parsePizza(exampleInputFile), IoUtils.parseSliceInstructions(exampleInputFile));
21-
23+
Pizza pizza = new Pizza(new File(EXAMPLE_INPUT_FILE_PATH), IoUtils.parsePizza(EXAMPLE_INPUT_FILE_PATH), IoUtils.parseSliceInstructions(EXAMPLE_INPUT_FILE_PATH));
2224
//then perform a step right from a particular cell
23-
DFSMethods.rightStep(pizza, new Slice(new Cell(1, 1, Ingredient.MUSHROOM)));
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);
2428
}
2529

2630
}

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,22 @@ private static List<Slice> createSlicesForParagonOutputExample() {
2828
Slice slice2 = new Slice();
2929

3030
slice0.cells.add(new Cell(0, 0, Ingredient.TOMATO));
31-
slice0.cells.add(new Cell(0, 1, Ingredient.TOMATO));
32-
slice0.cells.add(new Cell(0, 2, Ingredient.TOMATO));
3331
slice0.cells.add(new Cell(1, 0, Ingredient.TOMATO));
32+
slice0.cells.add(new Cell(2, 0, Ingredient.TOMATO));
33+
slice0.cells.add(new Cell(0, 1, Ingredient.TOMATO));
3434
slice0.cells.add(new Cell(1, 1, Ingredient.MUSHROOM));
35-
slice0.cells.add(new Cell(1, 2, Ingredient.TOMATO));
36-
37-
slice1.cells.add(new Cell(2, 0, Ingredient.TOMATO));
38-
slice1.cells.add(new Cell(2, 1, Ingredient.MUSHROOM));
35+
slice0.cells.add(new Cell(2, 1, Ingredient.TOMATO));
36+
37+
slice1.cells.add(new Cell(0, 2, Ingredient.TOMATO));
38+
slice1.cells.add(new Cell(1, 2, Ingredient.MUSHROOM));
3939
slice1.cells.add(new Cell(2, 2, Ingredient.TOMATO));
40-
41-
slice2.cells.add(new Cell(3, 0, Ingredient.TOMATO));
42-
slice2.cells.add(new Cell(3, 1, Ingredient.MUSHROOM));
43-
slice2.cells.add(new Cell(3, 2, Ingredient.TOMATO));
44-
slice2.cells.add(new Cell(4, 0, Ingredient.TOMATO));
45-
slice2.cells.add(new Cell(4, 1, Ingredient.TOMATO));
46-
slice2.cells.add(new Cell(4, 2, Ingredient.TOMATO));
40+
41+
slice2.cells.add(new Cell(0, 3, Ingredient.TOMATO));
42+
slice2.cells.add(new Cell(1, 3, Ingredient.MUSHROOM));
43+
slice2.cells.add(new Cell(2, 3, Ingredient.TOMATO));
44+
slice2.cells.add(new Cell(0, 4, Ingredient.TOMATO));
45+
slice2.cells.add(new Cell(1, 4, Ingredient.TOMATO));
46+
slice2.cells.add(new Cell(2, 4, Ingredient.TOMATO));
4747

4848
return Arrays.asList(slice0, slice1, slice2);
4949
}

0 commit comments

Comments
 (0)