Skip to content

Commit

Permalink
Merge pull request #10 from LyashenkoGS/DFSmethods
Browse files Browse the repository at this point in the history
Simplest working algorithms
  • Loading branch information
LyashenkoGS authored Jan 30, 2017
2 parents 8dd2c7a + fdd0e66 commit f6a17d0
Show file tree
Hide file tree
Showing 18 changed files with 866 additions and 94 deletions.
12 changes: 9 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#Java and Intellij specific
#Java and Intellij & Eclipse specific
.idea
*.iml
.project
.classpath
.project
target
#ignore the archived source code
*.zip
#ignore the archived source code and output files
/outputDataSet/*
*.zip
/.classpath
.settings/
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# GoogleHashCode2017

[![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)
![google hash code logo](./documentation/logo.png)
hash tags: #hashcode #2017 #googleHashCode
[![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)
[![Build Status](https://travis-ci.org/LyashenkoGS/GoogleHashCode2017.svg?branch=master)](https://travis-ci.org/LyashenkoGS/GoogleHashCode2017)

##Pizza
Practice problem for the Google HashCode 2017
Practice problem for the Google HashCode 2017. Current version works correctly for example input only.

* original assignment - [Task.pdf](./documentation/TaskDescription.pdf)
* [input data sets](./inputDataSets)
Expand Down Expand Up @@ -33,6 +34,6 @@ To automate interaction with online submission can be used [SeleniumIDE](https:/
with a firefox browser.
* login to the [submission page](https://hashcodejudge.withgoogle.com/#/rounds/6553823069863936/submissions/)
* setup selenium test suite(submitResultsViaSelenium) according to yours file system
* execute the test case and see scores on web
* 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)


4 changes: 2 additions & 2 deletions outputDataSet/example.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
3
0 0 2 1
0 2 2 2
0 3 2 4
0 3 2 4
0 0 2 1
15 changes: 15 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.7</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
</dependency>
</dependencies>

</project>
42 changes: 35 additions & 7 deletions src/main/java/com/google/hashcode/App.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,50 @@
package com.google.hashcode;

import com.google.hashcode.entity.Cell;
import com.google.hashcode.entity.Pizza;
import com.google.hashcode.entity.Slice;
import com.google.hashcode.entity.Step;
import com.google.hashcode.utils.DFSMethods;
import com.google.hashcode.utils.IoUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import static com.google.hashcode.utils.FilesPaths.*;


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

System.out.println("GoogleHashCode2017! Pizza task");
System.out.print(pizza);
public static void main(String[] args) throws IOException {
slicePizza(EXAMPLE_INPUT_FILE_PATH, OUTPUT_DATA_SET_EXAMPLE_TXT);
slicePizza(SMALL_INPUT_FILE_PATH, OUTPUT_DATA_SET_SMALL_TXT);
//TODO troubles to input big files, possible exciting String max size
//slicePizza(BIG_INPUT_FILE_PATH, OUTPUT_DATA_SET_BIG_TXT);
//slicePizza(MEDIUM_INPUT_FILE_PATH, OUTPUT_DATA_SET_MEDIUM_TXT);
}

public static void slicePizza(String inputFile, String outputFile) throws IOException {
List<Slice> output;
Pizza pizza = new Pizza(new File(inputFile), IoUtils.parsePizza(inputFile), IoUtils.parseSliceInstructions(inputFile));
//get start positions
output = DFSMethods.cutAllStartPositions(pizza);
//get All steps
Map<Slice, List<Step>> availableSteps = DFSMethods.getAvailableSteps(pizza, output);
while (!availableSteps.values().stream().allMatch(List::isEmpty)) {
Step step = DFSMethods.selectStep(availableSteps);
output.remove(step.startPosition);
output.add(DFSMethods.performStep(pizza, step));
//TODO available steps should include merging slices to each other
availableSteps = DFSMethods.getAvailableSteps(pizza, output);
LOGGER.info("OUTPUT AFTER A STEP: "
+ "\n " + output);
}
IoUtils.writeToFile(outputFile, IoUtils.parseSlices(output));
LOGGER.info("FINISHED for " + inputFile + "!!!!!");
}

}
35 changes: 30 additions & 5 deletions src/main/java/com/google/hashcode/entity/Cell.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,48 @@
package com.google.hashcode.entity;

import java.util.Objects;

/**
* Represents a pizza cell with it coordinates. There is no getters/setters for simplicity
*
* @author Grigoriy Lyashenko (Grog).
*/
public class Cell {
public final int x;
public final int y;
public final Ingredient ingredient;
public int y;
public int x;
public Ingredient ingredient;

public Cell(int x, int y, Ingredient ingredient) {
this.x = x;
public Cell(int y, int x, Ingredient ingredient) {
this.y = y;
this.x = x;
this.ingredient = ingredient;
}

@Override
public String toString() {
return ingredient.toString();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Cell)) return false;
Cell cell = (Cell) o;
return x == cell.x &&
y == cell.y &&
ingredient == cell.ingredient;
}

@Override
public int hashCode() {
return Objects.hash(x, y, ingredient);
}

public int getX() {
return x;
}

public int getY() {
return y;
}
}
71 changes: 61 additions & 10 deletions src/main/java/com/google/hashcode/entity/Pizza.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.google.hashcode.entity;

import com.google.hashcode.utils.IoUtils;

import java.io.File;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

/**
* Represents an immutable pizza
Expand All @@ -12,10 +13,10 @@
public class Pizza {

private final File input;
private final Cell[][] cells;
private final List<Cell> cells;
private final SliceInstruction sliceInstruction;

public Pizza(File input, Cell[][] cells, SliceInstruction sliceInstruction) {
public Pizza(File input, List<Cell> cells, SliceInstruction sliceInstruction) {
this.input = input;
this.cells = cells;
this.sliceInstruction = sliceInstruction;
Expand All @@ -25,20 +26,70 @@ public File getInput() {
return input;
}

public Cell[][] getCells() {
public List<Cell> getCells() {
return cells;
}

/**
* Coordinates are like in a 2D array
*
* @param y - row number, 0..max row number
* @param x - column number,0..max column number
* @return a pizza cell with specified coordinated
*/
public Optional<Cell> getCell(int y, int x) {
return cells.stream().filter(cell -> cell.x == x && cell.y == y).findFirst();
}

public SliceInstruction getSliceInstruction() {
return sliceInstruction;
}

@Override
public String toString() {
return input.toString() +
"\n" + IoUtils.convertToHumanReadableTable(cells) +
"\n" + sliceInstruction.toString();
return input.toString()
+ ("\n" + sliceInstruction.toString()
+ "\n" + outputCellsArray()).trim();
}

/**
* Indicates does this pizza contains each slice's cell
*
* @param slice given slice
* @return true if the pizza contains the slice
*/
public boolean containsCells(Slice slice) {
return slice.cells.stream().allMatch(this.cells::contains);
}

public SliceInstruction getSliceInstruction() {
return sliceInstruction;
private String outputCellsArray() {
if (!cells.isEmpty()) {
StringBuilder stringBuilder = new StringBuilder();
int columnsCount = cells.stream().max(Comparator.comparingInt(Cell::getX)).get().getX();
int rowsCount = cells.stream().max(Comparator.comparingInt(Cell::getY)).get().getY();
//output columns coordinates
stringBuilder.append(" ");
for (int column = 0; column < columnsCount + 1; column++) {
stringBuilder.append(" ").append(column);
}
stringBuilder.append("\n");
for (int row = 0; row < rowsCount + 1; row++) {
//output rows coordinates
stringBuilder.append(row).append(" ");
for (int column = 0; column < columnsCount + 1; column++) {
if (this.getCell(row, column).isPresent()) {
stringBuilder.append(this.getCell(row, column).get().toString()).append(" ");
} else {
stringBuilder.append(" ").append(" ");
}
}
stringBuilder.append("\n");
}
return stringBuilder.toString();
} else {
return "";
}
}


}
Loading

0 comments on commit f6a17d0

Please sign in to comment.