|
1 | 1 | package com.google.hashcode.utils; |
2 | 2 |
|
3 | 3 | 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; |
4 | 7 |
|
5 | 8 | import java.util.ArrayList; |
6 | | -import java.util.Collections; |
7 | | -import java.util.Comparator; |
8 | 9 | import java.util.List; |
| 10 | +import java.util.Optional; |
| 11 | +import java.util.stream.Collectors; |
9 | 12 |
|
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() { |
113 | 15 | } |
114 | | - //endregion |
115 | 16 |
|
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) { |
117 | 27 |
|
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; |
126 | 29 | } |
127 | 30 |
|
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))); |
149 | 38 |
|
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(); |
161 | 41 |
|
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(); |
172 | 43 | } |
173 | 44 |
|
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<>(); |
184 | 57 | } |
185 | 58 |
|
186 | | - //endregion |
187 | 59 | } |
0 commit comments