44import org .slf4j .Logger ;
55import org .slf4j .LoggerFactory ;
66
7- import java .util .*;
7+ import java .util .ArrayList ;
8+ import java .util .HashMap ;
9+ import java .util .List ;
10+ import java .util .Map ;
811import java .util .stream .Collectors ;
912
1013public abstract class DFSMethods {
@@ -13,44 +16,6 @@ public abstract class DFSMethods {
1316 private DFSMethods () {
1417 }
1518
16- /**
17- * Step as an entity is an amount of a pizza cells, that can be added to a slice of a pizza or a pizza cell.<br>
18- * Step as an action is a process of:<br>
19- * <p>
20- * * adding cells to a start cell or a start slice<br>
21- * * validate generated slice according to the pizza slicing instructions<br>
22- * * if validation passed ->cutting the start cell with added cells from the pizza
23- *
24- * @param pizza (mutable) a pizza to perform step on
25- * @param slice start position for a step
26- * @return cutted slice from the pizza
27- */
28- public static Optional <Slice > rightStep (Pizza pizza , Slice slice ) {
29- List <Cell > slicesRightBorder = slice .cells .stream ()
30- .filter (cell -> (cell .x == slice .maxX ()) && (cell .y >= slice .minY ()) && (cell .y <= slice .maxY ()))
31- .collect (Collectors .toList ());
32- //each cell should have a cell right side of it in the pizza
33- try {
34- Slice step = new Slice (slicesRightBorder .stream ()
35- .map (slicesRightBorderCell -> pizza .getCell (slicesRightBorderCell .y , slicesRightBorderCell .x + 1 ))
36- .collect (Collectors .toList ()));
37- //check is step is valid
38- Slice sliceAndStep = new Slice (new ArrayList (slice .cells ));
39- sliceAndStep .cells .addAll (step .cells );
40- if (!slice .cells .isEmpty () && sliceAndStep .isValid (pizza )) {
41- //remove the slice and step from the pizza
42- pizza .getCells ().removeAll (sliceAndStep .cells );
43- return Optional .of (sliceAndStep );
44- } else {
45- return Optional .empty ();
46- }
47- } catch (IllegalArgumentException e ) {
48- //if can't add at least one neccessary cell - > return an empty step
49- LOGGER .info ("Can't perform a step right !" );
50- return Optional .empty ();
51- }
52- }
53-
5419 /**
5520 * For each slice find all available steps. We DON'T change the pizza on this stage
5621 *
@@ -87,14 +52,24 @@ public static Map<Slice, List<Step>> getAvailableSteps(Pizza pizza, List<Slice>
8752 * @param steps available steps
8853 * @return formed slice that includes an original slice and delta from a step
8954 */
90- public static List < Step > performStep (Pizza pizza , Map <Slice , List <Step >> steps ) {
55+ public static Slice performStep (Pizza pizza , Map <Slice , List <Step >> steps ) {
9156 //1. Pick ups a steps list with minimal total cells number
92- final Optional <List <Step >> minStep = steps .values ().stream ().min ((o1 , o2 ) -> new StepsComparator ().compare (o1 , o2 ));
93- return minStep .get ();
57+ Step step = steps .values ().stream ().min ((o1 , o2 ) -> new StepsComparator ().compare (o1 , o2 )).get ().get (0 );
58+ LOGGER .info ("step to perform: " + step );
59+ //2. Cut all the step delta cells from pizza
60+ LOGGER .info ("pizza before step: " + pizza
61+ + "\n delta to remove from the pizza: " + step .delta );
62+ pizza .getCells ().removeAll (step .delta .cells );
63+ LOGGER .info ("pizza after step:" + pizza );
64+ //3. Add the step cells to an output slice
65+ Slice slice = new Slice (step .delta .cells );
66+ slice .cells .addAll (step .startPosition .cells );
67+ return slice ;
9468 }
9569
9670 /**
9771 * Finds a cells type with minimal cells numbers and generates one cell slices from them
72+ * Delete the slices from the pizza
9873 *
9974 * @param pizza given pizza
10075 * @return slices that are start positions for future slicing
@@ -109,15 +84,26 @@ public static List<Slice> cutAllStartPositions(Pizza pizza) {
10984 LOGGER .info ("cutAllStartPositions for pizza: " + pizza
11085 + "\n mushrooms number: " + mushrooms .size ()
11186 + "\n tomatoes number: " + tomatoes .size ());
87+ List <Slice > startPositions = null ;
11288 if (mushrooms .size () > tomatoes .size ()) {
113- return tomatoes .stream ()
89+ startPositions = tomatoes .stream ()
11490 .map (Slice ::new )
11591 .collect (Collectors .toList ());
92+ List <Cell > cellsToRemove = startPositions .stream ()
93+ .flatMap (slice -> slice .cells .stream ())
94+ .collect (Collectors .toList ());
95+ pizza .getCells ().removeAll (cellsToRemove );
11696 } else {
117- return mushrooms .stream ()
97+ startPositions = mushrooms .stream ()
11898 .map (Slice ::new )
11999 .collect (Collectors .toList ());
100+ List <Cell > cellsToRemove = startPositions .stream ()
101+ .flatMap (slice -> slice .cells .stream ())
102+ .collect (Collectors .toList ());
103+ pizza .getCells ().removeAll (cellsToRemove );
120104 }
105+ LOGGER .info ("pizza without start positions:" + pizza );
106+ return startPositions ;
121107 }
122108
123109}
0 commit comments