|
1 | 1 | package com.google.hashcode.utils; |
2 | 2 |
|
3 | | -import com.google.hashcode.entity.Cell; |
4 | | -import com.google.hashcode.entity.Ingredient; |
5 | | -import com.google.hashcode.entity.Pizza; |
6 | | -import com.google.hashcode.entity.Slice; |
7 | | -import com.google.hashcode.entity.Step; |
| 3 | +import com.google.hashcode.entity.*; |
8 | 4 | import org.slf4j.Logger; |
9 | 5 | import org.slf4j.LoggerFactory; |
10 | 6 |
|
11 | 7 | import java.util.ArrayList; |
12 | | -import java.util.Iterator; |
13 | 8 | import java.util.List; |
14 | 9 | import java.util.Optional; |
15 | 10 | import java.util.stream.Collectors; |
@@ -59,38 +54,61 @@ public static Optional<Slice> rightStep(Pizza pizza, Slice slice) { |
59 | 54 | } |
60 | 55 |
|
61 | 56 | /** |
62 | | - * Step as an action it's a process, when a slice adding to itself a subset of a pizza cells and remains rectangular |
63 | | - * and valid. |
| 57 | + * For each slice find all available steps. We DON'T change the pizza on this stage |
64 | 58 | * |
65 | 59 | * @param pizza given pizza |
66 | | - * @param output given slice in the pizza |
| 60 | + * @param output given slices in the pizza |
67 | 61 | * @return available steps |
68 | 62 | */ |
69 | | - List<Step> getAvailableSteps(Pizza pizza, List<Slice> output) { |
70 | | - //TODO implement.For each slice find all available steps. We DON'T change the pizza on this stage |
71 | | - return new ArrayList<>(); |
| 63 | + public static List<Step> getAvailableSteps(Pizza pizza, List<Slice> output) { |
| 64 | + List<Step> steps = new ArrayList<>(); |
| 65 | + for (Slice slice : output) { |
| 66 | + Slice stepLeftDelta = slice.generateStepDeltaLeft(); |
| 67 | + Slice stepRightDelta = slice.generateStepRight(); |
| 68 | + Slice stepAboveDelta = slice.generateStepDeltaAbove(); |
| 69 | + Slice stepBelowDelta = slice.generateStepDeltaBelow(); |
| 70 | + if (pizza.containsCells(stepLeftDelta)) steps.add(new Step(slice, stepLeftDelta)); |
| 71 | + if (pizza.containsCells(stepRightDelta)) steps.add(new Step(slice, stepRightDelta)); |
| 72 | + if (pizza.containsCells(stepAboveDelta)) steps.add(new Step(slice, stepAboveDelta)); |
| 73 | + if (pizza.containsCells(stepBelowDelta)) steps.add(new Step(slice, stepBelowDelta)); |
| 74 | + } |
| 75 | + LOGGER.info("available steps for" + |
| 76 | + "\npizza: " + pizza |
| 77 | + + "\nslices: " + output |
| 78 | + + "\nsteps: " + steps); |
| 79 | + return steps; |
72 | 80 | } |
73 | 81 |
|
74 | | - Slice performStep(Pizza pizza, List<Step> steps) { |
| 82 | + public static Slice performStep(Pizza pizza, List<Step> steps) { |
75 | 83 | //TODO pick-ups a step with a minimal steps number, execute it(cut it from the pizza, and a slice) |
76 | 84 | return null; |
77 | 85 | } |
78 | 86 |
|
79 | | - List<Slice> cutAllStartPositions(Pizza pizza) { |
80 | | - //TODO pick-ups a step with a minimal steps number, execute it(cut it from the pizza, and a slice). The pizza is a mutable object |
81 | | - List<Cell> currentPizza = pizza.getCells(); |
82 | | - List<Slice> starts = new ArrayList<Slice>(); |
83 | | - Iterator<Cell> iter = currentPizza.iterator(); |
84 | | - while(iter.hasNext()){ |
85 | | - Cell cell = iter.next(); |
86 | | - if(cell.ingredient == Ingredient.MUSHROOM){ |
87 | | - Slice slice = new Slice(); |
88 | | - slice.cells.add(cell); |
89 | | - starts.add(slice); |
90 | | - iter.remove(); |
91 | | - } |
92 | | - } |
93 | | - return starts; |
| 87 | + /** |
| 88 | + * Finds a cells type with minimal cells numbers and generates one cell slices from them |
| 89 | + * |
| 90 | + * @param pizza given pizza |
| 91 | + * @return slices that are start positions for future slicing |
| 92 | + */ |
| 93 | + public static List<Slice> cutAllStartPositions(Pizza pizza) { |
| 94 | + List<Cell> mushrooms = pizza.getCells().stream() |
| 95 | + .filter(cell -> cell.ingredient.equals(Ingredient.MUSHROOM)) |
| 96 | + .collect(Collectors.toList()); |
| 97 | + List<Cell> tomatoes = pizza.getCells().stream() |
| 98 | + .filter(cell -> cell.ingredient.equals(Ingredient.TOMATO)) |
| 99 | + .collect(Collectors.toList()); |
| 100 | + LOGGER.info("cutAllStartPositions for pizza: " + pizza |
| 101 | + + "\nmushrooms number: " + mushrooms.size() |
| 102 | + + "\ntomatoes number: " + tomatoes.size()); |
| 103 | + if (mushrooms.size() > tomatoes.size()) { |
| 104 | + return tomatoes.stream() |
| 105 | + .map(Slice::new) |
| 106 | + .collect(Collectors.toList()); |
| 107 | + } else { |
| 108 | + return mushrooms.stream() |
| 109 | + .map(Slice::new) |
| 110 | + .collect(Collectors.toList()); |
| 111 | + } |
94 | 112 | } |
95 | 113 |
|
96 | 114 | } |
0 commit comments