Skip to content

Commit 773455c

Browse files
committed
fixMediumPizzaSlicing. Fix getAllAvailableSteps for big arrays
1 parent ddbe18a commit 773455c

File tree

6 files changed

+32
-16
lines changed

6 files changed

+32
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
target
88
#ignore the archived source code and output files
99
/outputDataSet/*
10+
*.log
1011
*.zip
1112
/.classpath
1213
.settings/

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class App {
2424
public static void main(String[] args) throws IOException {
2525
slicePizza(EXAMPLE_INPUT_FILE_PATH, OUTPUT_DATA_SET_EXAMPLE_TXT);
2626
slicePizza(SMALL_INPUT_FILE_PATH, OUTPUT_DATA_SET_SMALL_TXT);
27-
slicePizza(MEDIUM_INPUT_FILE_PATH, OUTPUT_DATA_SET_MEDIUM_TXT);
27+
//slicePizza(MEDIUM_INPUT_FILE_PATH, OUTPUT_DATA_SET_MEDIUM_TXT);
2828
//TODO troubles to input big files, possible exciting String max size
2929
//slicePizza(BIG_INPUT_FILE_PATH, OUTPUT_DATA_SET_BIG_TXT);
3030
}
@@ -48,6 +48,9 @@ public static void slicePizza(String inputFile, String outputFile) throws IOExce
4848
}
4949
IoUtils.writeToFile(outputFile, IoUtils.parseSlices(output));
5050
LOGGER.info("FINISHED for " + inputFile + "!!!!!");
51+
LOGGER.info("sliced cells number: " + output.stream()
52+
.map(slice -> slice.cells.size())
53+
.reduce(0, (integer, integer2) -> integer + integer2));
5154
LOGGER.info(profiler.measure(inputFile + " execution time: "));
5255
}
5356

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public Step(Slice startPosition, Slice delta) {
2222
public boolean isValid(Pizza pizza) {
2323
Slice slice = new Slice(new ArrayList<>(startPosition.cells));
2424
slice.cells.addAll(delta.cells);
25-
return slice.isValid(pizza);
25+
return slice.isValid(pizza) ||
26+
slice.cells.size() < pizza.getSliceInstruction().getMaxNumberOfCellsPerSlice();
2627
}
2728

2829
public int size() {

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@ private DFSMethods() {
1515

1616
/**
1717
* For each slice find all available steps.<br>
18-
* If founded start position that haven't any steps and it is unvalid ->
19-
* remove this slice from startPositions and add all it's cells to pizza.
18+
* If used start position hasn't any steps and is invalid for a pizza ->
19+
* remove this slice from startPositions and add all it's cells to the pizza.
20+
*
2021
* @param pizza given pizza
2122
* @param startPositions given slices in the pizza
22-
* @param output
23+
* @param output list of valid slices
2324
* @return available steps
2425
*/
2526
public static Map<Slice, List<Step>> getAvailableSteps(Pizza pizza, List<Slice> startPositions, List<Slice> output) {
27+
Profiler profiler = new Profiler();
2628
Map<Slice, List<Step>> groupedSteps = new HashMap<>();
2729
Iterator iter = startPositions.iterator();
2830
while (iter.hasNext()) {
@@ -40,9 +42,9 @@ public static Map<Slice, List<Step>> getAvailableSteps(Pizza pizza, List<Slice>
4042
steps.add(stepAbove);
4143
steps = steps.stream().filter(Objects::nonNull).collect(Collectors.toList());
4244

43-
if (steps.size() == 0) {
45+
if (steps.isEmpty()) {
4446
if (startPosition.isValid(pizza)) {
45-
// if slice is valid and have'nt any steps -> cut it from
47+
// if slice is valid and haven't any steps -> cut it from
4648
// startPositions
4749
output.add(startPosition);
4850
iter.remove();
@@ -66,10 +68,10 @@ public static Map<Slice, List<Step>> getAvailableSteps(Pizza pizza, List<Slice>
6668
* Pick-ups a step with a minimal cells delta number,
6769
* execute it(cut it from the pizza, and add to a slice)
6870
*
69-
* @param pizza given pizza
70-
* @param step step to perform
71+
* @param pizza given pizza
72+
* @param step step to perform
7173
* @param startPositions
72-
*@param output @return formed slice that includes an original slice and delta from a step
74+
* @param output @return formed slice that includes an original slice and delta from a step
7375
*/
7476
public static void performStep(Pizza pizza, Step step, List<Slice> startPositions, List<Slice> output) {
7577
//1. Pick ups a steps list with minimal total cells number
@@ -89,9 +91,9 @@ public static void performStep(Pizza pizza, Step step, List<Slice> startPosition
8991
LOGGER.info("PIZZA AFTER STEP:" + pizza);
9092
//3. Add the step cells to an output slice
9193

92-
if(finalSlice.cells.size() == pizza.getSliceInstruction().getMaxNumberOfCellsPerSlice()){
94+
if (finalSlice.cells.size() == pizza.getSliceInstruction().getMaxNumberOfCellsPerSlice()) {
9395
output.add(finalSlice);
94-
} else{
96+
} else {
9597
startPositions.add(finalSlice);
9698
}
9799
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,9 @@ public static List<Cell> parsePizza(String file) throws IOException {
4040
List<Cell> cells = new ArrayList<>();
4141
int row = 0;
4242
String fileLine;
43-
int counter = 0;
4443
while ((fileLine = br.readLine()) != null) {
4544
for (int column = 0; column < fileLine.length(); column++) {
4645
Character literal = fileLine.charAt(column);
47-
counter ++;
48-
System.out.println("letter " + literal + " counter = " + counter);
4946
if (literal.toString().equals(Ingredient.TOMATO.toString())) {
5047
cells.add(new Cell(row, column, Ingredient.TOMATO));
5148
} else if (literal.toString().equals(Ingredient.MUSHROOM.toString())) {

src/main/resources/logback.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,24 @@
99
</layout>
1010
</appender>
1111

12+
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
13+
<file>./pizzaSlicing.log</file>
14+
<append>false</append>
15+
<encoder>
16+
<pattern>
17+
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
18+
</pattern>
19+
</encoder>
20+
</appender>
21+
1222
<root level="debug">
1323
<appender-ref ref="STDOUT"/>
1424
</root>
1525

16-
<logger name="com.google" level="debug"
26+
<logger name="com.google" level="info"
1727
additivity="false">
28+
<appender-ref ref="FILE"/>
1829
<appender-ref ref="STDOUT"/>
1930
</logger>
31+
2032
</configuration>

0 commit comments

Comments
 (0)