Skip to content

Commit 98f1987

Browse files
committed
Merge branch 'fixMediumPizzaSlicing' into refactoring
2 parents 58d634d + 56b46c9 commit 98f1987

File tree

13 files changed

+92
-608
lines changed

13 files changed

+92
-608
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/

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ hash tags: #hashcode #2017 #googleHashCode
55
[![Build Status](https://travis-ci.org/LyashenkoGS/GoogleHashCode2017.svg?branch=master)](https://travis-ci.org/LyashenkoGS/GoogleHashCode2017)
66

77
##Pizza
8-
Practice problem for the Google HashCode 2017. Current version works correctly for example input only.
8+
Practice problem for the Google HashCode 2017.
99

1010
* original assignment - [Task.pdf](./documentation/TaskDescription.pdf)
1111
* [input data sets](./inputDataSets)
@@ -30,6 +30,8 @@ To zip the source code execute :
3030

3131
./zipSourceCode.sh
3232

33+
34+
## Deprecated
3335
To automate interaction with online submission can be used [SeleniumIDE](https://addons.mozilla.org/en-US/firefox/addon/selenium-ide/)
3436
with a firefox browser.
3537
* login to the [submission page](https://hashcodejudge.withgoogle.com/#/rounds/6553823069863936/submissions/)

hs_err_pid9247.log

Lines changed: 0 additions & 554 deletions
This file was deleted.

outputDataSet/example.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
3
2+
1 2 2 2
3+
1 3 1 4
4+
1 0 1 1

pizzaSlicing.log

Lines changed: 0 additions & 3 deletions
This file was deleted.

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

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.google.hashcode.entity.Pizza;
44
import com.google.hashcode.entity.Slice;
55
import com.google.hashcode.entity.Step;
6-
import com.google.hashcode.utils.SlicingMethods;
76
import com.google.hashcode.utils.IoUtils;
87
import com.google.hashcode.utils.Profiler;
98
import org.slf4j.Logger;
@@ -15,39 +14,53 @@
1514
import java.util.List;
1615
import java.util.Map;
1716

18-
import static com.google.hashcode.utils.FilesPaths.*;
17+
import static com.google.hashcode.utils.FilesPaths.BIG_INPUT_FILE_PATH;
18+
import static com.google.hashcode.utils.FilesPaths.OUTPUT_DATA_SET_BIG_TXT;
19+
import static com.google.hashcode.utils.SlicingMethods.*;
1920

2021

2122
public class App {
2223
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
2324

2425
public static void main(String[] args) throws IOException {
25-
slicePizza(EXAMPLE_INPUT_FILE_PATH, OUTPUT_DATA_SET_EXAMPLE_TXT);
26-
slicePizza(SMALL_INPUT_FILE_PATH, OUTPUT_DATA_SET_SMALL_TXT);
27-
slicePizza(MEDIUM_INPUT_FILE_PATH, OUTPUT_DATA_SET_MEDIUM_TXT);
28-
//TODO troubles to input big files, possible exciting String max size
29-
//slicePizza(BIG_INPUT_FILE_PATH, OUTPUT_DATA_SET_BIG_TXT);
26+
//slicePizza(EXAMPLE_INPUT_FILE_PATH, OUTPUT_DATA_SET_EXAMPLE_TXT);
27+
//slicePizza(SMALL_INPUT_FILE_PATH, OUTPUT_DATA_SET_SMALL_TXT);
28+
//slicePizza(MEDIUM_INPUT_FILE_PATH, OUTPUT_DATA_SET_MEDIUM_TXT);
29+
slicePizza(BIG_INPUT_FILE_PATH, OUTPUT_DATA_SET_BIG_TXT);
3030
}
3131

32+
/**
33+
* Performs a pizza slicing
34+
*
35+
* @param inputFile given input pizza file
36+
* @param outputFile a file slicing results
37+
* @throws IOException cant parse a pizza file
38+
*/
3239
public static void slicePizza(String inputFile, String outputFile) throws IOException {
3340
Profiler profiler = new Profiler();
3441
List<Slice> startPositions;
3542
List<Slice> output = new ArrayList<>();
3643
Pizza pizza = new Pizza(new File(inputFile), IoUtils.parsePizza(inputFile), IoUtils.parseSliceInstructions(inputFile));
3744
//get start positions
38-
startPositions = SlicingMethods.cutAllStartPositions(pizza);
45+
startPositions = cutAllStartPositions(pizza);
3946
//get All steps
40-
Map<Slice, List<Step>> availableSteps = SlicingMethods.getAvailableSteps(pizza, startPositions, output);
47+
Map<Slice, List<Step>> availableSteps = getAvailableSteps(pizza, startPositions, output);
4148
while (!availableSteps.values().stream().allMatch(List::isEmpty)) {
42-
Step step = SlicingMethods.selectStep(availableSteps);
43-
SlicingMethods.performStep(pizza, step, startPositions, output);
44-
//TODO available steps should include merging slices to each other
45-
availableSteps = SlicingMethods.getAvailableSteps(pizza, startPositions, output);
49+
Step step = selectStep(availableSteps);
50+
performStep(pizza, step, startPositions, output);
51+
availableSteps = getAvailableSteps(pizza, startPositions, output);
4652
LOGGER.info("OUTPUT AFTER A STEP: "
4753
+ "\n " + output);
54+
LOGGER.info("start positions cells number: " + startPositions.stream()
55+
.map(slice -> slice.cells.size())
56+
.reduce(0, (integer, integer2) -> integer + integer2)
57+
);
4858
}
4959
IoUtils.writeToFile(outputFile, IoUtils.parseSlices(output));
5060
LOGGER.info("FINISHED for " + inputFile + "!!!!!");
61+
LOGGER.info("sliced cells number: " + output.stream()
62+
.map(slice -> slice.cells.size())
63+
.reduce(0, (integer, integer2) -> integer + integer2));
5164
LOGGER.info(profiler.measure(inputFile + " execution time: "));
5265
}
5366

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
public class Pizza {
1414

1515
private final File input;
16-
private final List<Cell> cells;
1716
private final SliceInstruction sliceInstruction;
17+
private List<Cell> cells;
1818

1919
public Pizza(File input, List<Cell> cells, SliceInstruction sliceInstruction) {
2020
this.input = input;
@@ -30,6 +30,10 @@ public List<Cell> getCells() {
3030
return cells;
3131
}
3232

33+
public void setCells(List<Cell> cells) {
34+
this.cells = cells;
35+
}
36+
3337
/**
3438
* Coordinates are like in a 2D array
3539
*

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public boolean isValid(Pizza pizza) {
116116
boolean isPassedSliceInstructions = this.cells.size() <= pizza.getSliceInstruction().getMaxNumberOfCellsPerSlice()
117117
&& tomatoesNumber >= pizza.getSliceInstruction().getMinNumberOfIngredientPerSlice()
118118
&& mushroomsNumber >= pizza.getSliceInstruction().getMinNumberOfIngredientPerSlice();
119-
LOGGER.info("\n" + pizza.getSliceInstruction() +
119+
LOGGER.debug("\n" + pizza.getSliceInstruction() +
120120
"\nSlice :" + this +
121121
"\npassed validation: " + isPassedSliceInstructions);
122122
return isPassedSliceInstructions;
@@ -132,7 +132,7 @@ public Step generateStepAbove(Pizza pizza) {
132132
if (cell.isPresent()) {
133133
delta.cells.add(cell.get());
134134
} else {
135-
LOGGER.info("cant perform step left !");
135+
LOGGER.debug("cant perform step left !");
136136
return null;
137137
}
138138
}
@@ -142,7 +142,7 @@ public Step generateStepAbove(Pizza pizza) {
142142
if (step.isValid(pizza)) {
143143
return step;
144144
} else {
145-
LOGGER.info("step is invalid !");
145+
LOGGER.debug("step is invalid !");
146146
return null;
147147
}
148148
}
@@ -155,7 +155,7 @@ public Step generateStepBelow(Pizza pizza) {
155155
if (cell.isPresent()) {
156156
delta.cells.add(cell.get());
157157
} else {
158-
LOGGER.info("cant perform step left !");
158+
LOGGER.debug("cant perform step left !");
159159
return null;
160160
}
161161
}
@@ -165,7 +165,7 @@ public Step generateStepBelow(Pizza pizza) {
165165
if (step.isValid(pizza)) {
166166
return step;
167167
} else {
168-
LOGGER.info("step is invalid !");
168+
LOGGER.debug("step is invalid !");
169169
return null;
170170
}
171171
}
@@ -178,7 +178,7 @@ public Step generateStepLeft(Pizza pizza) {
178178
if (cell.isPresent()) {
179179
delta.cells.add(cell.get());
180180
} else {
181-
LOGGER.info("cant perform step left !");
181+
LOGGER.debug("cant perform step left !");
182182
return null;
183183
}
184184
}
@@ -188,7 +188,7 @@ public Step generateStepLeft(Pizza pizza) {
188188
if (step.isValid(pizza)) {
189189
return step;
190190
} else {
191-
LOGGER.info("step is invalid !");
191+
LOGGER.debug("step is invalid !");
192192
return null;
193193
}
194194
}
@@ -201,7 +201,7 @@ public Step generateStepRight(Pizza pizza) {
201201
if (cell.isPresent()) {
202202
delta.cells.add(cell.get());
203203
} else {
204-
LOGGER.info("cant perform step right !");
204+
LOGGER.debug("cant perform step right !");
205205
return null;
206206
}
207207
}
@@ -211,7 +211,7 @@ public Step generateStepRight(Pizza pizza) {
211211
if (step.isValid(pizza)) {
212212
return step;
213213
} else {
214-
LOGGER.info("step is invalid !");
214+
LOGGER.debug("step is invalid !");
215215
return null;
216216
}
217217
}

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/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())) {

0 commit comments

Comments
 (0)