Skip to content

Commit f372081

Browse files
committed
DFSmethod. Add necessary methods to get pizza cell by coordinates, minor refactoring
1 parent 1bb368b commit f372081

File tree

9 files changed

+171
-206
lines changed

9 files changed

+171
-206
lines changed

src/main/java/com/google/hashcode/App.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
import java.io.IOException;
1212
import java.util.List;
1313

14+
import static com.google.hashcode.utils.InputFiles.EXAMPLE_INPUT_FILE_PATH;
15+
1416

1517
public class App {
1618
private static final Logger LOGGER = LoggerFactory.getLogger(Slicer.class);
1719

1820
public static void main(String[] args) throws IOException {
19-
String exampleInputFile = "inputDataSets/example.in";
20-
List<Cell> ingredients = IoUtils.parsePizza(exampleInputFile);
21-
Pizza pizza = new Pizza(new File(exampleInputFile), ingredients, IoUtils.parseSliceInstructions(exampleInputFile));
21+
Pizza pizza = new Pizza(new File(EXAMPLE_INPUT_FILE_PATH), IoUtils.parsePizza(EXAMPLE_INPUT_FILE_PATH), IoUtils.parseSliceInstructions(EXAMPLE_INPUT_FILE_PATH));
2222
// IoUtils.writeToFile("outputDataSet/example.txt", IoUtils.parseSlices(Slicer.slicePizza(pizza)));
2323
LOGGER.info("GoogleHashCode2017! Pizza task");
2424
LOGGER.info(pizza.toString());

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

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
* @author Grigoriy Lyashenko (Grog).
77
*/
88
public class Cell {
9-
public final int x;
10-
public final int y;
11-
public final Ingredient ingredient;
9+
public int x;
10+
public int y;
11+
public Ingredient ingredient;
1212
/**
1313
* indicates if given cell has been sliced
1414
*/
@@ -25,16 +25,8 @@ public String toString() {
2525
return ingredient.toString();
2626
}
2727

28-
/**
29-
* Creates a new cell based on the parent but add given coordinates
30-
* to the original one cell
31-
*
32-
* @param x delta x
33-
* @param y delta y
34-
* @return new Cell with adjusted coordinates
35-
*/
36-
public Cell prototype(int x, int y) {
37-
return new Cell(this.x + x, this.y + y, null);
28+
public static Cell prototype(int x, int y) {
29+
return new Cell(x, y, null);
3830
}
3931

4032
@Override
Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package com.google.hashcode.entity;
22

3-
import com.google.hashcode.utils.IoUtils;
4-
53
import java.io.File;
6-
import java.util.Arrays;
74
import java.util.List;
8-
import java.util.stream.Collectors;
5+
import java.util.Optional;
96

107
/**
118
* Represents an immutable pizza
@@ -16,6 +13,13 @@ public class Pizza {
1613

1714
private final File input;
1815
private final List<Cell> cells;
16+
private final SliceInstruction sliceInstruction;
17+
18+
public Pizza(File input, List<Cell> cells, SliceInstruction sliceInstruction) {
19+
this.input = input;
20+
this.cells = cells;
21+
this.sliceInstruction = sliceInstruction;
22+
}
1923

2024
public File getInput() {
2125
return input;
@@ -25,24 +29,31 @@ public List<Cell> getCells() {
2529
return cells;
2630
}
2731

28-
public SliceInstruction getSliceInstruction() {
29-
return sliceInstruction;
32+
/**
33+
* Coordinates are like in a 2D array
34+
*
35+
* @param y - row number, 0..max row number
36+
* @param x - column number,0..max column number
37+
* @return a pizza cell with specified coordinated
38+
*/
39+
public Cell getCell(int y, int x) {
40+
final Optional<Cell> cellByCoordinates = cells.stream().filter(cell -> cell.x == x && cell.y == y).findFirst();
41+
if (cellByCoordinates.isPresent()) {
42+
return cellByCoordinates.get();
43+
} else throw new IllegalArgumentException("No cell with "
44+
+ "\n y: " + y
45+
+ "\nx: " + x);
3046
}
3147

32-
private final SliceInstruction sliceInstruction;
33-
34-
public Pizza(File input, List<Cell> cells, SliceInstruction sliceInstruction) {
35-
this.input = input;
36-
this.cells = cells;
37-
this.sliceInstruction = sliceInstruction;
48+
public SliceInstruction getSliceInstruction() {
49+
return sliceInstruction;
3850
}
3951

4052
@Override
4153
public String toString() {
4254
return input.toString() +
43-
//TODO fix human readable output "\n" + IoUtils.convertToHumanReadableTable(cells) +
55+
//TODO fix human readable output "\n" + IoUtils.convertToHumanReadableTable(cells) +
4456
"\n" + sliceInstruction.toString();
4557
}
4658

47-
4859
}

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

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

33
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.Comparator;
46
import java.util.List;
57

68
/**
@@ -12,13 +14,32 @@ public class Slice {
1214

1315
public List<Cell> cells = new ArrayList<>();
1416

15-
public void add(Cell cell) {
16-
cells.add(cell);
17-
cell.sliced = true;
17+
public Slice() {
1818
}
1919

20-
public int cellsNumber() {
21-
return cells.size();
20+
public Slice(Cell cell) {
21+
this.cells = Collections.singletonList(cell);
22+
}
23+
24+
public int minX() {
25+
return Collections.min(cells, Comparator.comparingInt(Cell::getX)).x;
26+
}
27+
28+
public int minY() {
29+
return Collections.min(cells, Comparator.comparingInt(Cell::getX)).y;
30+
}
31+
32+
public int maxX() {
33+
return Collections.max(cells, Comparator.comparingInt(Cell::getX)).x;
34+
}
35+
36+
37+
/* public Slice(Cell cell) {
38+
this.cells = Collections.singletonList(cell);
39+
}*/
40+
41+
public int maxY() {
42+
return Collections.max(cells, Comparator.comparingInt(Cell::getX)).y;
2243
}
2344

2445
@Override
Lines changed: 40 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -1,187 +1,59 @@
11
package com.google.hashcode.utils;
22

33
import com.google.hashcode.entity.Cell;
4+
import com.google.hashcode.entity.Pizza;
5+
import com.google.hashcode.entity.Slice;
6+
import com.google.hashcode.entity.SliceInstruction;
47

58
import java.util.ArrayList;
6-
import java.util.Collections;
7-
import java.util.Comparator;
89
import java.util.List;
10+
import java.util.Optional;
11+
import java.util.stream.Collectors;
912

10-
public class DFSMethods {
11-
12-
public int CalculateNumberOfFreeCellsAroundSlice(List<Cell> slice, List<Cell> pizza) {
13-
int cellsAroundSlice = 0;
14-
//found coordinates of slice angles.
15-
int minX = findMinX(slice);
16-
int minY = findMinY(slice);
17-
int maxX = findMaxX(slice);
18-
int maxY = findMaxY(slice);
19-
//generate arrays of x and y coordinates for this slice.
20-
List<Integer> differenceXCoordinates = fillArrayWithIntValues(minX, maxX);
21-
List<Integer> differenceYCoordinates = fillArrayWithIntValues(minY, maxY);
22-
//checking top cells.
23-
if (didPizzaContainsAllNeededHorizontalCells(pizza, differenceXCoordinates, minY - 1))
24-
cellsAroundSlice += differenceXCoordinates.size();
25-
//checking left cells.
26-
if (didPizzaContainsAllNeededVerticalCells(pizza, differenceYCoordinates, maxX + 1))
27-
cellsAroundSlice += differenceYCoordinates.size();
28-
//checking bottom cells.
29-
if (didPizzaContainsAllNeededHorizontalCells(pizza, differenceXCoordinates, maxY + 1))
30-
cellsAroundSlice += differenceXCoordinates.size();
31-
//checking right cells.
32-
if (didPizzaContainsAllNeededVerticalCells(pizza, differenceYCoordinates, minX - 1))
33-
cellsAroundSlice += differenceYCoordinates.size();
34-
return cellsAroundSlice;
35-
}
36-
37-
//region steps
38-
39-
public boolean stepUp(List<Cell> slice, List<Cell> pizza, int maxCellsInSlice) {
40-
// finding min y coordinate in slice.
41-
Integer minYCoordInSlice = findMinY(slice);
42-
// find all difference x coordinates.
43-
List<Integer> differenceXCoordinates = fillAllDifferenceX(slice);
44-
//prevent this method from cutting too big slice.
45-
if (slice.size() + differenceXCoordinates.size() > maxCellsInSlice) return false;
46-
//our pizza still have all needed cells for this step? lets check it.
47-
boolean pizzaContainsAllNeededCells = didPizzaContainsAllNeededHorizontalCells(pizza, differenceXCoordinates, minYCoordInSlice - 1);
48-
if (pizzaContainsAllNeededCells) {
49-
//Cut all needed cells from pizza and add their to our slice.
50-
for (int xCoord : differenceXCoordinates) {
51-
//use .remove(index) to get object from pizza (with it's Ingradient)
52-
//but for getting those index use .indexOf(Object o)
53-
//TODO fix a compile time error. slice.add(pizza.remove(pizza.indexOf(new Cell(xCoord, minYCoordInSlice))))
54-
}
55-
return true;
56-
} else {
57-
//oups, pizza doesn't contain one of needed cells, step blocked.
58-
return false;
59-
}
60-
}
61-
62-
public boolean stepRight(List<Cell> slice, List<Cell> pizza, int maxCellsInSlice) {
63-
//TODO fix a compile time error
64-
/*
65-
Integer maxXCoordInSlice = findMaxX(slice);
66-
List<Integer> differenceYCoordinates = fillAllDifferenceY(slice);
67-
;
68-
if (slice.size() + differenceYCoordinates.size() > maxCellsInSlice) return false;
69-
boolean pizzaContainsAllNeededCells = didPizzaContainsAllNeededVerticalCells(pizza, differenceYCoordinates, maxXCoordInSlice + 1);
70-
if (pizzaContainsAllNeededCells) {
71-
for (int yCoord : differenceYCoordinates) {
72-
slice.add(pizza.remove(pizza.indexOf(new Cell(minXCoordInSlice, yCoord))))
73-
}
74-
return true;
75-
} else {
76-
return false;
77-
}*/
78-
return true;
79-
}
80-
81-
public boolean stepDown(List<Cell> slice, List<Cell> pizza, int maxCellsInSlice) {
82-
//TODO fix a compile time error
83-
/* Integer maxYCoordInSlice = findMaxY(slice);
84-
List<Integer> differenceXCoordinates = fillAllDifferenceX(slice);
85-
if (slice.size() + differenceXCoordinates.size() > maxCellsInSlice) return false;
86-
boolean pizzaContainsAllNeededCells = didPizzaContainsAllNeededVerticalCells(pizza, differenceXCoordinates, maxYCoordInSlice + 1);
87-
if (pizzaContainsAllNeededCells) {
88-
for (int xCoord : differenceXCoordinates) {
89-
slice.add(pizza.remove(pizza.indexOf(new Cell(xCoord, maxYCoordInSlice))))
90-
}
91-
return true;
92-
} else {
93-
return false;
94-
}*/
95-
return true;
96-
}
97-
98-
public boolean stepLeft(List<Cell> slice, List<Cell> pizza, int maxCellsInSlice) {
99-
//TODO fix a compile time error
100-
/* Integer minXCoordInSlice = findMinX(slice);
101-
List<Integer> differenceYCoordinates = fillAllDifferenceY(slice);
102-
if (slice.size() + differenceYCoordinates.size() > maxCellsInSlice) return false;
103-
boolean pizzaContainsAllNeededCells = didPizzaContainsAllNeededVerticalCells(pizza, differenceYCoordinates, minXCoordInSlice - 1);
104-
if (pizzaContainsAllNeededCells) {
105-
for (int yCoord : differenceYCoordinates) {
106-
slice.add(pizza.remove(pizza.indexOf(new Cell(minXCoordInSlice, yCoord))))
107-
}
108-
return true;
109-
} else {
110-
return false;
111-
}*/
112-
return true;
13+
public abstract class DFSMethods {
14+
private DFSMethods() {
11315
}
114-
//endregion
11516

116-
//region utils
17+
/**
18+
* Calculates a number of cells around the given slice. Available cells is cells that can be merged
19+
* into the slice, so it will remain a rectangle, so only up,down,left,right around an each slice outside cell.
20+
* No on the diagonal !
21+
*
22+
* @param slice given pizza
23+
* @param pizza given slice
24+
* @return number of cells available to merge into the slice
25+
*/
26+
public static int calculateNumberOfFreeCellsAroundSlice(Slice slice, Pizza pizza) {
11727

118-
private List<Integer> fillAllDifferenceX(List<Cell> slice) {
119-
List<Integer> diffX = new ArrayList<Integer>();
120-
for (Cell cell : slice) {
121-
if (!diffX.contains(cell.x)) {
122-
diffX.add(cell.x);
123-
}
124-
}
125-
return diffX;
28+
return 0;
12629
}
12730

128-
private List<Integer> fillAllDifferenceY(List<Cell> slice) {
129-
List<Integer> diffY = new ArrayList<Integer>();
130-
for (Cell cell : slice) {
131-
if (!diffY.contains(cell.y)) {
132-
diffY.add(cell.y);
133-
}
134-
}
135-
return diffY;
136-
}
137-
138-
private List<Integer> fillArrayWithIntValues(int start, int end) {
139-
List<Integer> returnedList = new ArrayList<>();
140-
for (int i = start; i < end + 1; i++) {
141-
returnedList.add((Integer) i);
142-
}
143-
return returnedList;
144-
}
145-
146-
private int findMinY(List<Cell> slice) {
147-
return Collections.min(slice, Comparator.comparingInt(Cell::getX)).y;
148-
}
31+
public static Optional<Slice> rightStep(Pizza pizza, Slice slice) {
32+
List<Cell> rightSlideBorder = slice.cells.stream()
33+
.filter(cell -> (cell.x == slice.maxX()) && (cell.y >= slice.minY()) && (cell.y <= slice.maxY()))
34+
.collect(Collectors.toList());
35+
//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)));
14938

150-
private int findMaxY(List<Cell> slice) {
151-
return Collections.max(slice, Comparator.comparingInt(Cell::getX)).y;
152-
}
153-
154-
private int findMinX(List<Cell> slice) {
155-
return Collections.min(slice, Comparator.comparingInt(Cell::getX)).x;
156-
}
157-
158-
private int findMaxX(List<Cell> slice) {
159-
return Collections.max(slice, Comparator.comparingInt(Cell::getX)).x;
160-
}
39+
//check is step is valid
40+
SliceInstruction sliceInstruction = pizza.getSliceInstruction();
16141

162-
private boolean didPizzaContainsAllNeededHorizontalCells(List<Cell> pizza, List<Integer> differenceXCoordinate, Integer yCoord) {
163-
//TODO fix a compile time error
164-
/* boolean returnValue = true;
165-
for (Integer xCoord : differenceXCoordinate) {
166-
if (!pizza.contains(new Cell(xCoord, yCoord))) {
167-
returnValue = false;
168-
}
169-
}
170-
return returnValue;*/
171-
return true;
42+
return Optional.empty();
17243
}
17344

174-
private boolean didPizzaContainsAllNeededVerticalCells(List<Cell> pizza, List<Integer> differenceYCoordinate, Integer xCoord) {
175-
//TODO fix a compile time error
176-
/* boolean returnValue = true;
177-
for (Integer yCoord : differenceYCoordinate) {
178-
if (!pizza.contains(new Cell(xCoord, yCoord))) {
179-
returnValue = false;
180-
}
181-
}
182-
return returnValue;*/
183-
return true;
45+
/**
46+
* Step as an action it's a process, when a slice adding to itself a subset of a pizza cells and remains rectangular.
47+
* Step as an entity is a Slice tha can be added to a particular slice inside a particalur pizza
48+
* considering slice instructions
49+
*
50+
* @param pizza given pizza
51+
* @param slice given slice in the pizza
52+
* @param sliceInstruction restrictions for a steps
53+
* @return available steps
54+
*/
55+
List<Slice> getAvailableSteps(Pizza pizza, Slice slice, SliceInstruction sliceInstruction) {
56+
return new ArrayList<>();
18457
}
18558

186-
//endregion
18759
}

0 commit comments

Comments
 (0)