Skip to content

Commit f6a17d0

Browse files
authored
Merge pull request #10 from LyashenkoGS/DFSmethods
Simplest working algorithms
2 parents 8dd2c7a + fdd0e66 commit f6a17d0

File tree

18 files changed

+866
-94
lines changed

18 files changed

+866
-94
lines changed

.gitignore

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
#Java and Intellij specific
1+
#Java and Intellij & Eclipse specific
22
.idea
33
*.iml
4+
.project
5+
.classpath
6+
.project
47
target
5-
#ignore the archived source code
6-
*.zip
8+
#ignore the archived source code and output files
9+
/outputDataSet/*
10+
*.zip
11+
/.classpath
12+
.settings/

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# GoogleHashCode2017
2-
3-
[![Join the chat at https://gitter.im/GoogleHashCode2017/Lobby](https://badges.gitter.im/GoogleHashCode2017/Lobby.svg)](https://gitter.im/GoogleHashCode2017/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
42
![google hash code logo](./documentation/logo.png)
3+
hash tags: #hashcode #2017 #googleHashCode
4+
[![Join the chat at https://gitter.im/GoogleHashCode2017/Lobby](https://badges.gitter.im/GoogleHashCode2017/Lobby.svg)](https://gitter.im/GoogleHashCode2017/Lobby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
5+
[![Build Status](https://travis-ci.org/LyashenkoGS/GoogleHashCode2017.svg?branch=master)](https://travis-ci.org/LyashenkoGS/GoogleHashCode2017)
56

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

910
* original assignment - [Task.pdf](./documentation/TaskDescription.pdf)
1011
* [input data sets](./inputDataSets)
@@ -33,6 +34,6 @@ To automate interaction with online submission can be used [SeleniumIDE](https:/
3334
with a firefox browser.
3435
* login to the [submission page](https://hashcodejudge.withgoogle.com/#/rounds/6553823069863936/submissions/)
3536
* setup selenium test suite(submitResultsViaSelenium) according to yours file system
36-
* execute the test case and see scores on web
37+
* execute the test case and see scores on web. See the [video instruction on YouTube](https://www.youtube.com/watch?v=Wg7s3CtIeCs&feature=youtu.be)
3738

3839

outputDataSet/example.txt

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

pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@
2424
<version>4.12</version>
2525
<scope>test</scope>
2626
</dependency>
27+
<dependency>
28+
<groupId>org.slf4j</groupId>
29+
<artifactId>slf4j-api</artifactId>
30+
<version>1.7.21</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>ch.qos.logback</groupId>
34+
<artifactId>logback-core</artifactId>
35+
<version>1.1.7</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>ch.qos.logback</groupId>
39+
<artifactId>logback-classic</artifactId>
40+
<version>1.1.7</version>
41+
</dependency>
2742
</dependencies>
2843

2944
</project>
Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,50 @@
11
package com.google.hashcode;
22

3-
import com.google.hashcode.entity.Cell;
43
import com.google.hashcode.entity.Pizza;
4+
import com.google.hashcode.entity.Slice;
5+
import com.google.hashcode.entity.Step;
6+
import com.google.hashcode.utils.DFSMethods;
57
import com.google.hashcode.utils.IoUtils;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
610

711
import java.io.File;
812
import java.io.IOException;
13+
import java.util.List;
14+
import java.util.Map;
15+
16+
import static com.google.hashcode.utils.FilesPaths.*;
917

1018

1119
public class App {
12-
public static void main(String[] args) throws IOException {
13-
String exampleInputFile = "inputDataSets/example.in";
14-
Cell[][] ingredients = IoUtils.parsePizza(exampleInputFile);
15-
Pizza pizza = new Pizza(new File(exampleInputFile), ingredients, IoUtils.parseSliceInstructions(exampleInputFile));
20+
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
1621

17-
System.out.println("GoogleHashCode2017! Pizza task");
18-
System.out.print(pizza);
22+
public static void main(String[] args) throws IOException {
23+
slicePizza(EXAMPLE_INPUT_FILE_PATH, OUTPUT_DATA_SET_EXAMPLE_TXT);
24+
slicePizza(SMALL_INPUT_FILE_PATH, OUTPUT_DATA_SET_SMALL_TXT);
25+
//TODO troubles to input big files, possible exciting String max size
26+
//slicePizza(BIG_INPUT_FILE_PATH, OUTPUT_DATA_SET_BIG_TXT);
27+
//slicePizza(MEDIUM_INPUT_FILE_PATH, OUTPUT_DATA_SET_MEDIUM_TXT);
28+
}
1929

30+
public static void slicePizza(String inputFile, String outputFile) throws IOException {
31+
List<Slice> output;
32+
Pizza pizza = new Pizza(new File(inputFile), IoUtils.parsePizza(inputFile), IoUtils.parseSliceInstructions(inputFile));
33+
//get start positions
34+
output = DFSMethods.cutAllStartPositions(pizza);
35+
//get All steps
36+
Map<Slice, List<Step>> availableSteps = DFSMethods.getAvailableSteps(pizza, output);
37+
while (!availableSteps.values().stream().allMatch(List::isEmpty)) {
38+
Step step = DFSMethods.selectStep(availableSteps);
39+
output.remove(step.startPosition);
40+
output.add(DFSMethods.performStep(pizza, step));
41+
//TODO available steps should include merging slices to each other
42+
availableSteps = DFSMethods.getAvailableSteps(pizza, output);
43+
LOGGER.info("OUTPUT AFTER A STEP: "
44+
+ "\n " + output);
45+
}
46+
IoUtils.writeToFile(outputFile, IoUtils.parseSlices(output));
47+
LOGGER.info("FINISHED for " + inputFile + "!!!!!");
2048
}
2149

2250
}
Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,48 @@
11
package com.google.hashcode.entity;
22

3+
import java.util.Objects;
4+
35
/**
46
* Represents a pizza cell with it coordinates. There is no getters/setters for simplicity
57
*
68
* @author Grigoriy Lyashenko (Grog).
79
*/
810
public class Cell {
9-
public final int x;
10-
public final int y;
11-
public final Ingredient ingredient;
11+
public int y;
12+
public int x;
13+
public Ingredient ingredient;
1214

13-
public Cell(int x, int y, Ingredient ingredient) {
14-
this.x = x;
15+
public Cell(int y, int x, Ingredient ingredient) {
1516
this.y = y;
17+
this.x = x;
1618
this.ingredient = ingredient;
1719
}
1820

1921
@Override
2022
public String toString() {
2123
return ingredient.toString();
2224
}
25+
26+
@Override
27+
public boolean equals(Object o) {
28+
if (this == o) return true;
29+
if (!(o instanceof Cell)) return false;
30+
Cell cell = (Cell) o;
31+
return x == cell.x &&
32+
y == cell.y &&
33+
ingredient == cell.ingredient;
34+
}
35+
36+
@Override
37+
public int hashCode() {
38+
return Objects.hash(x, y, ingredient);
39+
}
40+
41+
public int getX() {
42+
return x;
43+
}
44+
45+
public int getY() {
46+
return y;
47+
}
2348
}
Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package com.google.hashcode.entity;
22

3-
import com.google.hashcode.utils.IoUtils;
4-
53
import java.io.File;
4+
import java.util.Comparator;
5+
import java.util.List;
6+
import java.util.Optional;
67

78
/**
89
* Represents an immutable pizza
@@ -12,10 +13,10 @@
1213
public class Pizza {
1314

1415
private final File input;
15-
private final Cell[][] cells;
16+
private final List<Cell> cells;
1617
private final SliceInstruction sliceInstruction;
1718

18-
public Pizza(File input, Cell[][] cells, SliceInstruction sliceInstruction) {
19+
public Pizza(File input, List<Cell> cells, SliceInstruction sliceInstruction) {
1920
this.input = input;
2021
this.cells = cells;
2122
this.sliceInstruction = sliceInstruction;
@@ -25,20 +26,70 @@ public File getInput() {
2526
return input;
2627
}
2728

28-
public Cell[][] getCells() {
29+
public List<Cell> getCells() {
2930
return cells;
3031
}
3132

33+
/**
34+
* Coordinates are like in a 2D array
35+
*
36+
* @param y - row number, 0..max row number
37+
* @param x - column number,0..max column number
38+
* @return a pizza cell with specified coordinated
39+
*/
40+
public Optional<Cell> getCell(int y, int x) {
41+
return cells.stream().filter(cell -> cell.x == x && cell.y == y).findFirst();
42+
}
43+
44+
public SliceInstruction getSliceInstruction() {
45+
return sliceInstruction;
46+
}
3247

3348
@Override
3449
public String toString() {
35-
return input.toString() +
36-
"\n" + IoUtils.convertToHumanReadableTable(cells) +
37-
"\n" + sliceInstruction.toString();
50+
return input.toString()
51+
+ ("\n" + sliceInstruction.toString()
52+
+ "\n" + outputCellsArray()).trim();
3853
}
3954

55+
/**
56+
* Indicates does this pizza contains each slice's cell
57+
*
58+
* @param slice given slice
59+
* @return true if the pizza contains the slice
60+
*/
61+
public boolean containsCells(Slice slice) {
62+
return slice.cells.stream().allMatch(this.cells::contains);
63+
}
4064

41-
public SliceInstruction getSliceInstruction() {
42-
return sliceInstruction;
65+
private String outputCellsArray() {
66+
if (!cells.isEmpty()) {
67+
StringBuilder stringBuilder = new StringBuilder();
68+
int columnsCount = cells.stream().max(Comparator.comparingInt(Cell::getX)).get().getX();
69+
int rowsCount = cells.stream().max(Comparator.comparingInt(Cell::getY)).get().getY();
70+
//output columns coordinates
71+
stringBuilder.append(" ");
72+
for (int column = 0; column < columnsCount + 1; column++) {
73+
stringBuilder.append(" ").append(column);
74+
}
75+
stringBuilder.append("\n");
76+
for (int row = 0; row < rowsCount + 1; row++) {
77+
//output rows coordinates
78+
stringBuilder.append(row).append(" ");
79+
for (int column = 0; column < columnsCount + 1; column++) {
80+
if (this.getCell(row, column).isPresent()) {
81+
stringBuilder.append(this.getCell(row, column).get().toString()).append(" ");
82+
} else {
83+
stringBuilder.append(" ").append(" ");
84+
}
85+
}
86+
stringBuilder.append("\n");
87+
}
88+
return stringBuilder.toString();
89+
} else {
90+
return "";
91+
}
4392
}
93+
94+
4495
}

0 commit comments

Comments
 (0)